Color

From Xojo Documentation

Data Type


The data type for storing color values with an optional alpha component. The default value of a Color is &c000000 (black).

Constants
Black Gray Red
Blue Green Teal
Brown LightGray White
Clear Magenta Yellow
Cyan Orange
DarkGray Purple
Properties
Alpha fa-lock-32.png Hue fa-lock-32.png Value fa-lock-32.png
Blue fa-lock-32.png Magenta fa-lock-32.png Yellow fa-lock-32.png
Cyan fa-lock-32.png Red fa-lock-32.png
Green fa-lock-32.png Saturation fa-lock-32.png
Shared Methods
CMY HSV RGB
DarkBevelColor HSVA RGBA
DarkTingeColor HighlightColor SelectedFromDialog
DisabledTextColor IsDarkMode TextColor
FillColor LightBevelColor
FrameColor LightTingeColor

Notes

Colors are often used to assign colors to properties of type Color. Use the RGB, HSV, or CMY functions to assign a color to an object or use the format:

&cRRGGBB

In the above, RR is the RGB value of Red in hexadecimal, GG is the value Green in hexadecimal, and BB is the value of Blue in hexadecimal. You can right-click in the Code Editor and select Insert Color to choose a color using the color picker, which will insert the hexadecimal value at the cursor position in the editor.

Desktop, Web and Console projects can also use the CMY function to set the color using cyan, magenta and yellow elements. The CMY function is not available for iOS projects.

You can use the VarType function to determine whether a property or a variable is a Color. If it is, VarType returns 16.

Color Values to/from Hex Strings

Desktop, Web and Console projects can use the Str global function to get the hexadecimal RGB value of a Color:
Var c As Color
c = Color.RGB(255, 100, 50)
Var hexColor As String = Str(c)
To convert a hexadecimal color value back to a Color, you first have to convert it to a 32-bit Integer (Int32 or UInt32) and then cast it to a Color like this:
// hexColor is a String that contains a hex color value
Var intColor As Int32 = hexColor.Val // Can also use UInt32
Var c2 As Color = Color(intColor)

Alpha Channel Support

The Color type has a read-only "Alpha" property. The alpha channel is the transparency of the color represented as an integer between 0 (opaque) and 255 (transparent).

The Color literal syntax can optionally have two more hexadecimal digits at the end representing the alpha channel (for example, "&cFF00007F"). If the color literal has only 3 channels of information, the alpha component defaults to 0.

&cRRGGBBAA

The RGB, HSV, and CMY functions also have an optional integer parameter that specifies the alpha channel for the Color.

Sample Code

The following example uses the RGB model to set the DrawingColor property of a Canvas control and draw a square using the current DrawingColor. The code is placed in the Canvas control's Paint event.

g.DrawingColor = Color.RGB(255, 0, 0)
g.DrawRectangle(0, 0, 50, 50)

The following example (in a Canvas Paint event) assigns a color directly using the RGB color model. The RGB values must be specified in hexadecimal:

g.DrawingColor = &cFF0000
g.DrawRectangle(0, 0, 50, 50)

The following example uses the CMY model to set the fillcolor of a Rectangle control, r.

r.FillColor = Color.CMY(0.1, 0.3, 0.5)

The following example inverts the fillcolor:

Var c As Color
c=Color.RGB(255 - r.FillColor.Red, 255 - r.FillColor.Green, 255 - r.FillColor.Blue)
r.FillColor = c

See Also

Str, Val, VarType functions; &c literal; ColorGroup class