Table of Contents

23.4.2.1 Colour classes (gb.qt4 and gb.image)

This static class Color (gb.qt4) has properties that return the system colours. Please note that in Gambas there is also a class Color (gb.image) in addition to the class Color (gb.qt4). This static class Color (gb.image) defines constants of predefined colours and contains some useful methods to deal with colours. The fact is that the colour constants and the useful methods are in Color (gb.image) and the system colours are in Color (gb.qt4). The Colour (gb.image) class contains constants and methods that do not depend on specific libraries. The constants are in RGB format. The methods use algorithms written or adapted by Minisini. The system colours, on the other hand, are linked to the current system and are provided by the current toolkit.

23.4.2.1.1 Class Colour (gb.qt4) - Properties

The (static) properties are described in the following table:

Colour nameDecimalhexaDecimal
Background15724527&HEFEFEF
ButtonBackground14540772&HDDDFE4
ButtonForeground0&H0
Foreground0&H0
LightBackground13885419&HD3DFEB
LightForeground10921381&HA6A5A5
SelectedBackground6786482&H678DB2
SelectedForeground16777215&HFFFFFF
TextBackground16777215&HFFFFFF
TextForeground0&H0
TooltipBackground16777180&HFFFFDC
TooltipForeground0&H0

Table 23.4.2.1.1: Colour constants class Color (gb.qt4)

23.4.2.1.2 Class Colour (gb.image) - Constants

This class Color (gb.image) is static and defines these colour constants:

Colour nameDecimalhexaDecimalRGBHSV
Default - Transparent-1&HFFFFFFFF--
Black0&H0000000,0,00,0,0
White16777217&HFFFFFF255,255,2550,0,255
Gray8421504&H808080128,128,1280,0,128
DarkGray4210752&H40404064,64,640,0,64
LightGray12632256&HC0C0C0192,192,1920,0,192
Red16711680&HFF0000255,0,00,255,255
DarkRed8388608&H800000128,0,00,255,128
Green65280&H00FF000,255,0120,255,255
DarkGreen32768&H0080000,128,0120,255,128
Blue255&H0000FF0,0,255240,255,255
DarkBlue128&H0000800,0,128240,255,128
Yellow16776960&HFFFF00255,255,060,255,255
DarkYellow8421376&H808000128,128,060,255,128
Orange16744448&HFF8000255,128,030,255,255
Magenta16711935&HFF00FF255,0,255300,255,255
DarkMagenta8388736&H800080128,0,128300,255,128
Cyan65535&H00FFFF0,255,255180,255,255
DarkCyan32896&H0080800,128,128180,255,128
Pink16744703&HFF80FF255,128,255300,127,255
Violet8388863&H8000FF128,0,255270,255,255

Table 23.4.2.1 : Colour constants of the static class Color (gb.image)

23.4.2.1.3 Class Colour (gb.image) - Methods

The numerous methods of the class Color (gb.image) enable the user to work with colours in Gambas in a qualified manner.

ColourDescription
RGB ( Red AS Integer, Green AS Integer, Blue AS Integer [ , Alpha AS Integer ] ) AS IntegerReturns a colour value from the RGB colour space with its red, green and blue colour component.
HSV ( Hue AS Integer, Saturation AS Integer, Value AS Integer [ , Alpha AS Integer ] ) AS IntegerReturns a colour value from the HSV colour space with its hue, saturation and brightness colour components.
SetRGB ( Color As Integer [ , Red As Integer, Green As Integer, Blue As Integer, Alpha As Integer ] ) As IntegerThe set colour is optionally changed by the four new RGBA colour components. If at least one of these four colour components is specified, the corresponding colour component of the set colour is replaced by the specified parameter. If no colour component is specified, the set colour remains unchanged.
SetHSV ( Colour As Integer [ , Hue As Integer, Saturation As Integer, Value As Integer, Alpha As Integer ] ) As IntegerThe set colour is optionally changed by the four new HSVA colour components. If at least one of these four colour components is specified, the corresponding colour component of the set colour is replaced by the specified parameter. If no colour component is specified, the set colour remains unchanged.
SetAlpha ( Color As Integer, Alpha As Integer ) As IntegerChanges the alpha value of a colour and returns the new colour.
GetAlpha ( Color As Integer ) As IntegerReturns the alpha value of the specified colour.
Darker ( Color As Integer ) As IntegerReturns a darker shade of the existing colour.
Lighter ( Color As Integer ) As IntegerReturns a lighter shade of the existing colour.
Blend ( Source As Integer, Destination As Integer ) As IntegerBlends the source colour and the destination colour depending on the alpha channel of the two colours and returns the resulting colour. The alpha channel of the resulting colour is the most transparent alpha channel of the source and destination.
Merge ( Color1 As Integer, Color2 As Integer [ , Weight As Float ] ) As IntegerReturns a mixture of Color1 and Color2. Weight is the relative weight of the first colour, which is between 0 and 1. If the weight is not specified, it is set to 0.5.
Desaturate ( Color As Integer ) As IntegerReturns the specified colour (desaturated) as a shade of grey.
Gradient ( Color1 As Integer, Color2 As Integer [ , Weight As Float ] ) As IntegerDepending on the weight, a colour between Color1 and Color2 is returned. If the weight is 0, the colour Color1 is returned. If the weight is 1, the colour Color2 is returned. If no weight is specified, the weight is set to 0.5.
Distance (Color1 As Integer, Color2 As Integer) As FloatReturns the RGB distance between two colours as a floating point number between 0.0 and 1.0.

Table 23.4.2.1.2: Methods of the Colour (gb.image) class

23.4.2.1.4 Examples for the use of methods of the class Colour (gb.image)

In the selected examples, the original colours and the changed colours are displayed in two ColorButtons or three ColorChoosers and the source code used is specified.

Example 1 - Colour.Darker(..)

B1

Public Sub btnDarker_Click()
 
    Dim iSourceColor, iResultColor As Integer
 
    iSourceColor = ColorButton1.Color
    iResultColor = Color.Darker(iSourceColor)
    ColorButton2.Color = iResultColor
'-- ColorButton2.Color = Color.Darker(ColorButton1.Color) '-- Short version
 
End

Example 2 - Colour.Lighter(..)

B2

Public Sub btnLighter_Click()
  ColorButton2.Color =  Color.Lighter(ColorButton1.Color)
End

Example 3 - Colour.Desaturate(..)

B3

Public Sub btnDesaturate_Click()
  ColorButton2.Color =  Color.Desaturate(ColorButton1.Color)
End

Example 4 - Colour.Distance(..)

B4

Public Sub btnDistance_Click()
 
  Dim iColor1, iColor2 As Integer
  Dim fDistance As Float
 
  iColor1 = ColorButton1.Color
  iColor2 = ColorButton2.Color
 
  fDistance = Color.Distance(iColor1, iColor2)
  Print fDistance
 
End

With the two colours red (Color.RGB(255,0,0)) and green (Color.HSV(120,255,255)), this results in a distance value of 0.70710678118655. Isn't that nice! In any case, the author is thrilled - but can't do anything with this value.

Example 5 - Colour.Merge(..)

B5

Public Sub btnMerge_Click()
 
    Dim iColor1, iColor2, iResultColor As Integer
    Dim fWeight As Float
 
'-- Weight is the relative weight of the first color, between 0 and 1.
    iColor1 = ColorButton1.Color
    fWeight = 0.3
    iColor2 = ColorButton2.Color
 
    iResultColor = Color.Merge(iColor1, iColor2, fWeight)
'-- iResultColor = Color.Merge(iColor1, iColor2) ' ---> fWeight = 0.5 (Default)
    ColorButton3.Color = iResultColor
 
End

Example 6 - Colour.Gradient(..)

B6

Public Sub btnGradient_Click()
 
    Dim iColor1, iColor2, iResultColor As Integer
    Dim fWeight As Float
 
    iColor1 = ColorButton1.Color
    iColor2 = ColorButton2.Color
'-- fWeight = 0.0 ' Sonderfall 1
'-- fWeight = 1.0 ' Sonderfall 2
    fWeight = 0.4
 
    iResultColor = Color.Gradient(iColor1, iColor2, fWeight)
'-- iResultColor = Color.Gradient(iColor1, iColor2) ' ---> fWeight = 0.5 (Default)
    ColorButton3.Color = iResultColor
 
End

Example 7 - Colour.Blend(..)

B

Public Sub btnBlend_Click()
 
    Dim iSourceColor, iDestinationColor, iResultColor As Integer
 
    iSourceColor = ColorButton1.Color
    iSourceColor = Color.SetAlpha(iSourceColor, 190)
    ColorChooser1.SelectedColor = iSourceColor
 
    iDestinationColor = ColorButton2.Color
    iDestinationColor = Color.SetAlpha(iDestinationColor, 90)
    ColorChooser2.SelectedColor = iDestinationColor
 
    iResultColor = Color.Blend(iSourceColor, iDestinationColor)
    Print Color.GetAlpha(iResultColor)
'-- Alternative: Print Color[iResultColor].Alpha
    ColorChooser3.SelectedColor = iResultColor
 
End