Gambas/Colours

From Wikibooks, open books for an open world
< Gambas
Jump to: navigation, search

Back to Gambas

Basic colour constants that may be used to specify colour[edit]

Hexadecimal Value Colour
&H0 Black
&HFF Red
&HFF00 Green
&HFFFF Yellow
&HFF0000 Blue
&HFF00FF Magenta
&HFFFF00 Cyan
&HFFFFFF White

The RGB Colours Program[edit]

This program shows you the RGB colours. You can change the values of each colour from 0 - 255 and you will see the special mixed colour, when you hit the commandbutton.

Out of 3 decimal numbers, one hexadecimal number is build as a string using the following code:

sHEX = Hex$(r,2) & Hex$(g,2) & Hex$(b,2)
sHEX = "&H" & sHEX & "&"

To get the program going , you will need 1 commandbutton, 1 drawingarea, 3 textboxes and 3 textlabels.

What does it look like? See it here ( in German ) http://www.madeasy.de/7/prgfarbergb.htm

The Code:

PUBLIC SUB Button1_Click()
DIM sHex AS String
DIM r AS Integer
DIM g AS Integer
DIM b AS Integer
IF Textbox1.text = "" THEN Textbox1.Text = 0 
IF Textbox2.text = "" THEN Textbox2.Text = 0 
IF Textbox3.text = "" THEN Textbox3.Text = 0 
r = Val(Textbox1.Text)
g = Val(Textbox2.Text)
b = Val(Textbox3.Text)
sHEX = Hex$(r,2) & Hex$(g,2) & Hex$(b,2)
sHEX = "&H" & sHEX & "&"
DrawingArea1.BackColor = Val(sHEX)
END 

The Sunset Program[edit]

When you need a background for a program, you can use this little color sunset program.

You will need a drawing area on your form to get the program going. You can choose different colours when you use the following scheme:

Blue RGB(0, 0, i) Red RGB(i, 0, 0) Green RGB(0, i, 0) Yellow RGB(i, i, 0) Violett RGB(i, 0, i) Türkis RGB(0, i, i) Gray RGB(i, i, i)

What does it look like ?

The Code

PUBLIC SUB Form_Open()
DIM r AS Integer
DIM g AS Integer
DIM b AS Integer
DIM h AS Integer
DIM he AS Integer
DIM sHex AS String
r = 0
g = 0
FOR b = 0 TO 255 
sHEX = Hex$(r,2) & Hex$(g,2) & Hex$(b,2)
sHEX = "&H" & sHEX & "&"
Draw.begin(DrawingArea1)
Draw.Forecolor= Val(sHEX)
Draw.LineWidth=5
he = DrawingArea1.Height
h = he * b \ 255
Draw.Line(0, h, DrawingArea1.Width, h)
Draw.End
NEXT
END

You can make the program better, when you add the following code and change the border of the form to Resizeable.

PUBLIC SUB Form_Resize()
DrawingArea1.Move(0, 0, ME.ClientWidth, ME.ClientHeight)
END

The Spectrum program[edit]

The natural colour spectrum is shown in the following program using a slidebar.

You need a drawing area and a slidebar as controls.

' Gambas class file
PUBLIC SUB Form_Open()
drawingarea1.BackColor = &HC2020C&
scrollbar1.minvalue = 1
scrollbar1.maxvalue = 1120
END
PUBLIC SUB ScrollBar1_Change()
drawingarea1.BackColor = Val(spektrum(scrollbar1.value))
END
PUBLIC FUNCTION spektrum(ss AS Integer) AS String
x AS Integer
sHEX AS String
s AS Integer 
r AS Integer
g AS Integer
b AS Integer
s = 0
FOR x = 1  TO 255
'red to yellow
r = 255
b = 0
sHEX = Hex$(r,2) & Hex$(x,2) & Hex$(b,2)
sHEX = "&H" & sHEX & "&"
s = s + 1 
'PRINT s
IF s = ss THEN RETURN sHEX
NEXT
's = 255
FOR x = 255  TO 1 STEP -1
'from yellow to green 
g = 255
b = 0
sHEX = Hex$(x,2) & Hex$(g,2) & Hex$(b,2)
sHEX = "&H" & sHEX & "&"
s = s + 1 
'PRINT s
IF s = ss THEN RETURN sHEX
NEXT
's = 510
FOR x = 1  TO 255
'from green to cyan 
r = 0
g = 255
sHEX = Hex$(r,2) & Hex$(g,2) & Hex$(x,2)
sHEX = "&H" & sHEX & "&"
s = s + 1 
'PRINT s
IF s = ss THEN RETURN sHEX
NEXT
's = 765
FOR x = 255  TO 1 STEP - 1
'from cyan to blue 
r = 0
b = 255
sHEX = Hex$(r,2) & Hex$(x,2) & Hex$(b,2)
sHEX = "&H" & sHEX & "&"
s = s + 1 
'PRINT s
IF s = ss THEN RETURN sHEX
NEXT
's = 1020
b = 255
FOR x = 1  TO 100
'from blue to violett
g = 0
sHEX = Hex$(x,2) & Hex$(g,2) & Hex$(b,2)
sHEX = "&H" & sHEX & "&"
b = b - 2
s = s + 1 
'PRINT s
'PRINT shex
IF s = ss THEN RETURN sHEX
NEXT  
's = 1120
END

Maybe you can change the program, so that the colour value is shown in a textbox. 1. Get a textbox out of the toolbox. 2. Add the following code

textbox1.text = spektrum(scrollbar1.value)

in the part PUBLIC SUB ScrollBar1_Change()