Custom Colors Stored in Database

Hi Xojo Gurus
Is there a way to store the RGB values of a colour, in a database. Then extract the RGB value to construct a colour. I can create the colour directly in code eg
g.DrawingColor = Color.RGB(229,184,184)
But I can’t find a way to extract values from variables.
Any advice would be fantastic.
Cheers
Dale

I think you have what you search here: Color

This is the code I was using in a class. Is there anything that you can see why it shouldn’t work?

Var rowRS As RowSet
Var loopCounter As Integer = 0

rowRS = GetColours

For Each row As DatabaseRow In rowRS

if row.Column(“Severity_Name”).StringValue = “Severe” Then
SevereColour = Color.RGB(row.Column(“Red”).IntegerValue, row.Column(“Green”).IntegerValue, row.Column(“Blue”).IntegerValue)
elseif row.Column(“Severity_Name”).StringValue = “High” Then
HighColour = Color.RGB(row.Column(“Red”).IntegerValue, row.Column(“Green”).IntegerValue, row.Column(“Blue”).IntegerValue)
end
Next
rowRS.Close
AlreadyCreated = True

You use a loop to get one colour (OK: two colours); a single call is enough.

What says the debugger for SevereColour and/or HighColour ?

If you get garbage, define an atom color for each R, G and B variable and set the values from the DB to them, then read them in the Debugger…

Dim aRed As Integer
aRed = row.Column(“Red”).IntegerValue

For debugging purposes…

Thanks Emile, will try breaking it down

We store the RGB values as a single integer:

Function RGB2Int(pRGBColor As Color) As Int32
  Dim theIntColour As Int32
  
  theIntColour = pRGBColor.Red * 65536
  theIntColour = theIntColour + pRGBColor.Green * 256
  theIntColour = theIntColour + pRGBColor.Blue
  
  Return theIntColour
End Function
Function Int2RGB(pIntColour As Int32) As Color
  Dim theRGB As Color
  Dim r, g, b As Int32
  
  r = pIntColour / 65536
  pIntColour = pIntColour - (r * 65536)
  
  g = pIntColour / 256
  pIntColour = pIntColour - (g * 256)
  
  b = pIntColour
  
  theRGB = RGB(r, g, b)
  
  Return theRGB
End Function

Thanks Emile and Kevin for your great ideas. I knew as soon as I asked the question, there would be some fantastic ideas. It seems I just had to tweak a few settings and it all now works.
The class stores the colours, and now I can reference the property of the class. Just had to break it down.
Thanks from this newby for all your ideas and help. Really appreciate it.

You can simply store it as a string and recover it afterwards like this, via a variant

Storing:

`row.Column(“Severity_Color”).StringValue = cstr(thecolor) 
 //thecolor is a color  .. what is stored looks like  &h00FF00FF .. the hex`

Recovering:

dim cu as variant
cu= row.Column(“Severity_Color”).StringValue
g.forecolor = cu.ColorValue
1 Like

Especially if you want to also store the alpha value.

Or using an example straight from the Documentation

Use the ToString function to get the hexadecimal RGB value of a Color:

Var c1 As Color
c1 = Color.RGB(255, 100, 50)
Var hexColor As String = c1.ToString // hexColor = &h00FF6432
Note that a hex Color has the alpha value at the front instead of the end like an &c Color does.

To convert a hexadecimal Color value back to a Color, use the FromString shared method like this:

// hexColor is the String from above that contains a hex color value
Var c2 As Color = Color.FromString(hexColor)

So you not have to fool with Variants :slightly_smiling_face:

1 Like

Use a variant as that will make it easy to convert a color to a string and back again.

I am not trying to be argumentative. I just want clarification. Why isn’t this basically as simple as it can be. What is the advantage of using a Variant?

// turn a color into a String

Var hexColor As String = c1.ToString

// turn a string back into a color

Var c2 As Color = Color.FromString(hexColor)
2 Likes

Those methods are API2, relatively new, (2020r1). many users are not aware ot those and others like me cant use because of last licence is from before that so, variant it is.

But yes, having a current licence those are simpler and easier.

3 Likes

Like Ivan says, I (like lots of others) am using API1… the color. method didn’t exist.

I have a lot of colours that I want to use. So I turned them into a method instead of a class. This is using API2.

The method can be called anywhere a colour can be used:
g.DrawingColor = AllColoursModule.rSevere

Where rSevere is colour created from RGB values stored in a database:
rSevere = Color.RGB(row.Column(“Colour_Red”).IntegerValue, row.Column(“Colour_Green”).IntegerValue, row.Column(“Colour_Blue”).IntegerValue)

The colours are only created once during the application session, and only just before the first time they are needed.

Interesting but since you are using API2 wouldn’t it be simpler just to store a single string?

Create the string and recover your colors this way?

With the advantage of also storing the alpha channel if ever relevant for your use

Var hexColor As String = c1.ToString
Var c2 As Color = Color.FromString(hexColor)

Robert, so the benefit would be only needing one field in the database. I like it. Thanks

Isn’t it a bit like trying to kill a fly with an elephant gun ?

A series of Properties in a Module will be far easier…

I would use the html colour names as properties in a rColours module. But if you’re happy now…