Hello,
I have noticed that some Xojo code segments use RGB values, and yet others use Hexadecimal triplet codes.
Is there any advantage of using one over the other, or is it simply down to personal preference?
In other words, are there any circumstances where one type of colour code must be used, or can either type of colour code always be used?
Also, if one type of colour code can be used anywhere / everywhere - is there any reason to use one type over the other?
Thank you.
Example 1 using RGB:
[code] // SET ALTERNATING ROW COLOURS
if row mod 2 <> 0 then
g.ForeColor=RGB(255,255,255)
else
g.ForeColor=RGB(240,246,255)
end if
g.FillRect 0,0,g.width,g.height[/code]
Example 2 using Hex Codes:
// SET THE SELECTED ROW COLOUR
If Me.Selected(row) Then
g.ForeColor = &cADC4E1
g.FillRect (0, 0, g.Width, g.Height)
Return True
End If
Benchmark shows about a 20% time savings using the literal vs RGB function. Both are fast enough to not be a concern generally… I had to do a loop of 10 million iterations to see a measurable difference.
apart from the performance differences, I’m using more and more the Hex format, once I discovered the possibility to quickly insert the hex code, by right clicking the contextual popup menu anywhere inside the code window.
I generally use the literal when the value is fixed and the RGB function when it is variable obviously one has to use RGB (or HSV etc.) when the value is variable.
I think there has been a feature request to calculate some things at compile time. e.g. chr(65) or asc(“A”). Or math functions like sqrt. But current compiler may not do that.
I use the hex one if I have color as hex (E.g. copied from html). If I have it as decimal numbers, I use RGB.