Small Basic – About Hexadecimal Pen and Brush Colors


You can set RGB colors using the BrushColor property of the GraphicsWindow object.

Every color’s made up of three values, written in the hexadecimal number system (or hex, for short). In decimal, the number system you’re probably most familiar with, each digit can have a value from 0 to 9; in hex, you can still use digits 0 to 9, but you can also use A, B, C, D, E, and F, representing the decimal numbers 10, 11, 12, 13, 14, and 15.

In an RGB color, each pair of hex digits represents a color value. From left to right, those are red (R), green (G), and blue (B), as shown in the Figure below. Together, these three components make a color identifier.

Figure: Selecting a color by its red, green, and blue components

As you change those digits, you’re actually changing a color! For example, in the Figure above, the color value 0000FF gives you pure blue, because FF is the highest a pair of hex digits can be and the other two pairs are just zeroes. The Figure also shows how you’d make green and red; what color identifier would you use to make purple? Try different combinations of digits and see what colors you can make!

Note: We’ve covered all you need to know to get started with colors in hexadecimal, but if you want to learn more, head to Small Basic: Color for an overview and to Small Basic Getting Started Guide: Appendix B: Colors for a list of hexadecimal colors!

But if you don’t like the hex format, you can just use the GetColorFromRGB() method, instead:

GraphicsWindow.GetColorFromRGB(red,green,blue)

The three arguments of the GetColorFromRGB() method set the red, green, and blue values of your color. You can pass any three values between 0 and 255. The higher the value, the brighter your color! The method returns a color identifier that you can use to set the brush or pen color. For example, this next statement sets the pen’s color to bright yellow by combining red and green:

GraphicsWindow.PenColor=GraphicsWindow.GetColorFromRGB(255,255,0)

Another method that makes dealing with colors easy is GetRandomColor(). This method doesn’t have any arguments; it returns a randomly selected color. Go ahead and try it out! 

Learn more about Small Basic: http://blogs.msdn.com/b/smallbasic/

 

Small and Basically yours,

    - Ninja Ed & Majed Marji

Comments (2)

  1. Computers Today (part 1 of 6)

    blogs.msdn.com/…/computers-today.aspx

    …..

    CS SPOTLIGHT: Girls in computer programming… why it matters!!!

    blogs.msdn.com/…/cs-spotlight-girls-in-computer-programming-why-it-matters.aspx

    Computational Thinking – Videos & Papers by Jeannette Wing

    blogs.msdn.com/…/computational-thinking-videos-amp-papers-by-jeannette-wing.aspx

Skip to main content