this is to show, how some #Define's can make a big difference (for the programmer)!
Tests made: FBC 1.05.0 WIN 32 (-gen gas) and FBC 1.05.0 WIN 64 (-gen gcc), aswell as:
FBC (current) 1.06.0 WIN 32 (-gen gas) and FBC (current) 1.06.0 WIN 64 (-gen gcc).
This is about Color (ULong, aka: unsigned 32-bit, int-type) and the matter of change, or
get the value, based on Alpha-/Color-Channels (the single UBytes, contained in a ULong).
All together, there are 8 #Define's, 4 x getter's and, 4 x setter's.
Getter's first, with some 'proof of concept' code (probably less used, than the Setter's):
<Edit=2018-04-12> Getter's needed a change, see Paul Doe's post and my reply. Code updated. </Edit>
<Edit> above comment landed in the code section, sorry! </Edit>
Code: Select all
' Color-Channel_getDef.bas --2018-04-11, MrSwiss
'
' compile: -s gui
'
' color definitions
Const As ULong orange = &hBFFF7F00, white = &hFFFFFFFF, black = &hFF000000, _
red = &h00FF0000, green = &h0000FF00, blue = &h000000FF
' screen definitions
Const As Short scr_w = 480, scr_h = 160, clr_d = 32
' function like MACRO's (getters) for alpha-/color-channel's values (UByte, from 32-bit color)
#Define ga_ch(c) ( CULng(c) Shr 24 And 255 ) ' get alpha-channel
#Define gr_ch(c) ( CULng(c) Shr 16 And 255 ) ' get red-channel
#Define gg_ch(c) ( CULng(c) Shr 8 And 255 ) ' get green-channel
#Define gb_ch(c) ( CULng(c) And 255 ) ' get blue-channel
' proof of concept code
ScreenRes(scr_w, scr_h, clr_d) ' graphic screen definition
Width scr_w \ 8, scr_h \ 16 ' 8 x 16 Font
Color(orange, white) : Cls ' set FG, BG colors
Dim As UByte b, g, r, a ' local variables (LSB to MSB) for channel's
' using the MACRO's for assignment (base is: const orange)
b = gb_ch(orange) : g = gg_ch(orange)
r = gr_ch(orange) : a = ga_ch(orange)
' show results to user ...
Print " Color-Channel #Define's test: String 'orange'"
Color(black) ' change FG color (after title)
Print " using the Color-Channel getter's on: 32bit orange!"
Print
Print " alpha: ", a; " (dec)", Hex(a, 2); " (hex)"
Color(red)
Print " red : ", r; " (dec)", Hex(r, 2); " (hex)"
Color(green)
Print " green: ", g; " (dec)", Hex(g, 2); " (hex)"
Color(blue)
Print " blue : ", b; " (dec)", Hex(b, 2); " (hex)" ' <-- cheating a bit!
Print : Color(black)
Print " press a key, to exit ... ";
Sleep
' proof of concept code - end ' ----- EOF -----
Setter's with some 'proof of concept' code:
Code: Select all
' Color-Channel_setDef.bas --2018-04-11, MrSwiss
'
' compile: -s gui
'
' color definitions
Const As ULong orange = &hBFFF7F00, white = &hFFFFFFFF, black = &hFF000000, _
red = &h00FF0000, green = &h0000FF00, blue = &h000000FF
' screen definitions
Const As Short scr_w = 480, scr_h = 480, clr_d = 32
' function like MACRO's (setters) for alpha-/color-channel's (32-bit color)
#Define sa_ch(c,a) ( CULng(c) + ((a And 255) Shl 24) ) ' set alpha-channel
#Define sr_ch(c,r) ( CULng(c) + ((r And 255) Shl 16) ) ' set red-channel
#Define sg_ch(c,g) ( CULng(c) + ((g And 255) Shl 8) ) ' set green-channel
#Define sb_ch(c,b) ( CULng(c) + (b And 255) ) ' set blue-channel
' getter use (see: Color-Channel_getDef.bas)
#Define gg_ch(c) ( CULng(c) Shr 8 And 255 ) ' get green-channel
' proof of concept code
ScreenRes(scr_w, scr_h, clr_d) ' graphic screen definition
Width scr_w \ 8, scr_h \ 16 ' 8 x 16 Font
Color(orange, white) : Cls ' set FG, BG colors
Dim As ULong c1, c2, c3, c4 ' local variables for color's
' proof of: same result as RGBA(). Here, we have to use modified var. as souce
' in the steps, following the first one! Otherwise, loss of prev. assignment(s).
c1 = sa_ch(0, 255) : c1 = sr_ch(c1, 127) : c1 = sg_ch(c1, 0) : c1 = sb_ch(c1, 127)
c2 = RGBA(127, 0, 127, 255) ' alpha is last here (above: its first = MSB)
' show it
Color(c1) : Print " c1's value: "; c1; " set with Channel MACRO's"
Color(c2) : Print " c2's value: "; c2; " set with RGBA()"
' here we change one channel in a loop (usefulness test)
Color(black)
Print : Print " green Channel changes in a loop:"
Print " original's are: c3 = red, c4 = blue (alpha = 0)"
' use in a loop, demo ... IMPORTANT: don't use modified var. as source!
' unless, you want 'doubling' behaviour! (harder to calculate)
Print " forwards ... from: 0 --> 255 (&h00 --> &hFF)"
For i As UInteger = 0 To 5 ' 255 \ 5 = 51 (multiplier factor, used below)
c3 = sg_ch(red, i * 51) : c4 = sg_ch(blue, i * 51)
Color(c3) : Print " c3's value: "; c3, Hex(c3, 8),
Color(black) : Print Hex(gg_ch(c3), 2); " green-ch"
Color(c4) : Print " c4's value: "; c4, Hex(c4, 8),
Color(black) : Print Hex(gg_ch(c4), 2); " green-ch"
Next
Print " and, backwards ... from: 255 --> 0 (&hFF --> &h00)"
For i As Integer = 5 To 0 Step -1 ' a UInteger, causes a 'endless loop'!
c3 = sg_ch(red, i * 51) : c4 = sg_ch(blue, i * 51)
Color(c3) : Print " c3's value: "; c3, Hex(c3, 8),
Color(black) : Print Hex(gg_ch(c3), 2); " green-ch"
Color(c4) : Print " c4's value: "; c4, Hex(c4, 8),
Color(black) : Print Hex(gg_ch(c4), 2); " green-ch"
Next
Print : Print " press a key, to exit ... ";
Sleep
' proof of concept code - end ' ----- EOF -----
(And no, I don't want to use 'Or' in the #Define's, I prefer '+' !)
I'll post an updated "GFX_MATH.bi" also, in its own thread ... (containing now: all the #Define's).