Small Basic: Pixel
This article is about pixel in Microsoft Small Basic language.
A pixel is the smallest element of a digital picture. An abbreviation px is used for pixels as a measure of resolution. Default GraphicsWindow size is 624 pixels wide and 441 pixels high. In HTML (CSS), 1 px is defined as 1/96 of 1 in (inch).
There are following two operations in GraphicsWindow object. Note that pixels in TextWindow can not be controlled:
- GetPixel - gets the color of the pixel
- SetPixel - draws the pixel
The following code gets the color where the mouse is clicked and shows it in the title. This code shows "#000000" which means black. But the GraphicsWindow is white. Why? This is because GetPixel operation returns foreground color. By default, background color is white and foreground color is transparent black. So, GetPixel returns black. For more detail about these layers, see this article. It also describes the reason why GetPixel can not get Shapes or Controls color.
GraphicsWindow.MouseDown = OnMouseDown
Sub OnMouseDown
x = GraphicsWindow.MouseX
y = GraphicsWindow.MouseY
color = GraphicsWindow.GetPixel(x, y)
GraphicsWindow.Title = color
EndSub
Following code sets random color to random pixel in GraphicsWindow:
GraphicsWindow.BackgroundColor = "Black"
gw = GraphicsWindow.Width
gh = GraphicsWindow.Height
While "True"
x = Math.GetRandomNumber(gw) - 1
y = Math.GetRandomNumber(gh) - 1
color = GraphicsWindow.GetRandomColor()
GraphicsWindow.SetPixel(x, y, color)
EndWhile
Following programs use GetPixel and SetPixel operations:
When a published program runs in web browser, following issues may occur.
- Small Basic Known Issue: 23257 - GraphicsWindow.GetPixel() Returns Alpha Blending Color in Remote
- Small Basic Known Issue: 26992 - GraphicsWindow.GetPixel(X, Y) Doesn’t Work Properly If X Or Y Has after the Decimal Point in Remote