Color Gradient in a Listbox

After much tinkering I managed to get a gradient, but I would like it to go from left to right, from the color chosen as the background it should fade towards white… or at least a color more towards white. The code is this:

' RowTag of ROW
If row >= 0 And row < ListBoxEventi.LastRowIndex+1 Then
  
  Var tag As String = ListBoxEventi.RowTagAt(row)
  
  If tag <> "" Then
    Var colori() As String = tag.Split(",")
    
    Dim startColor As Color =Color.FromString(colori(0))
    Dim endColor As Color = color.white
    Dim p as New Picture(g.Width, g.Height)
    Dim samt, eamt As Double
    
    For i As Integer = 0 To p.Width
      samt = 1 - (i / p.width)
      eamt = i / p.width
      p.Graphics.ForeColor = RGB((startColor.Red * samt) + (endColor.Red * eamt), (startColor.Green *samt) + (endColor.Green * eamt), (startColor.Blue * samt) + (endColor.Blue * eamt))
      p.Graphics.DrawLine(i,-1,i, p.width+1)
    Next
    
    g.DrawPicture(p, 0, 0)
    
    
  End If
else
  g.DrawingColor= color.White  'WHITE
  g.FillRectangle(0, 0, g.Width, g.Height)
  
  
end if


I’m experimenting, a solution that I might like, in the meantime I’ll try things that I’ve never tried before :slight_smile:

I would like to smoke or understand how to go from the color chosen as the background color, to white to apply it to the individual boxes, thus having the first cell with the chosen color, the second with the chosen color minus something and so on, up to the last almost white box.

I hope I’ve managed to make myself clear.

have a look at LinearGradientBrush, it should exists many years in xojo.

1 Like

You will need to calculate the starting and ending gradient colors based on the width of each column and the total width of all columns.
The code you posted already does that in some way.

And as Markus pointed, using LinearGradientBrush will allow for faster drawing as it is handled by the operating system.

I was curious to get this working.

From what I understand, you want a continuous gradient from left to right that looks like this:

Here is a demo project:

Listbox gradient.xojo_binary_project.zip (6.2 KB)

8 Likes

WOW, it’s really beautiful !! that’s what i had in mind, perfect. i’ll study the code and apply it to my project, thanks again.

HELP !!

Dim startColor As Color = UInt32ToColor(color.FromString(colori(0))) return error: Eventi.ListBoxEventi.PaintCellBackground, line 11
Parameter “value” expects type UInt32, but this is type Color.
Dim startColor As Color = UInt32ToColor(color.FromString(colori(0))) 'color.FromString(colori(0)) 'Color.FromString(colori(0))

The color read is:&h00ACAC59

Function UInt32ToColor(value As UInt32) As Color
  Var alpha As Integer = Bitwise.ShiftRight(Bitwise.BitAnd(value, &hFF000000), 24)
  Var red   As Integer = Bitwise.ShiftRight(Bitwise.BitAnd(value, &h00FF0000), 16)
  Var green As Integer = Bitwise.ShiftRight(Bitwise.BitAnd(value, &h0000FF00), 8)
  Var blue  As Integer = Bitwise.BitAnd(value, &h000000FF)
  
  Return Color.RGBA(red, green, blue, alpha)
End Function

Color.FromString returns a Color, not a UInt32. Try:

  Dim startColor As Color = color.FromString(colori(0))

Errror: is not a hex color value. Be sure to use the &h prefix.

What is:

colori(0)

is a string? does it has the &h?

Looks like the error you posted evaluated UInt32ToColor but your use of color.FromString is wrong as colori(0) is not a valid string that can be used in color.FromString?

there is some exception that I am missing… I read the following values: &h000080FF, &h00ACAC59 and then I get the error: is not a hex color value. Be sure to use the &h prefix.

A constant for a hex colour would be &c00123456. Is that perhaps what you are thinking of.

Il colore applicato nell’esempio è : &cF0E7DA, il mio invece viene memorizzato come: &h00ACAC59

Here’s how it works:
Dim colorValue As UInt32 = uint32.FromHex(“00ACAC59”) '&h00ACAC59
If I read the value "&h00ACAC59 " as a string from my database, I can’t assign it to colorValue.

colors(0)=“&h00804040”
integer.FromHex(colors(0)) ERROR
integer.FromString(color(0)) ERROR

I solved:
Dim colorValue As UInt32
if colori(0)<>“” then
colorvalue= uint32.FromHex(colori(0).ReplaceAll(“&h”,“”)) '&h00ACAC59
else
colorvalue=uint32.FromHex(“FFFFFFFF”) 'WHITE
end if

I don’t know if this is the right way, but it works. Suggestions to improve the code?

When pasting code please highlight it and press the </> button, it makes it so much easier. Equally it prevents the " from being turned into curley quotes that don’t work in Xojo.

This works:

Dim colorValue As UInt32
if colori(0)<>"" then
   colorvalue= uint32.FromHex(colori(0).ReplaceAll("&h","")) '&h00ACAC59
else
   colorvalue=uint32.FromHex("FFFFFFFF") 'WHITE
end if

This doesn’t, due to “” being changed:

Dim colorValue As UInt32
if colori(0)<>“” then
   colorvalue= uint32.FromHex(colori(0).ReplaceAll(“&h”,“”)) '&h00ACAC59
else
   colorvalue=uint32.FromHex(“FFFFFFFF”) 'WHITE
end if

Separately, colours are not integers. They are a different class. Whilst they seem like a UInt32 they are not. &c is the prefix for a colour constant, &h is a prefix for a numeric constant in hex format. The two are not interchangeable.

2 Likes

ok, thanks.