none
Draw multiple lines with color gradient RRS feed

  • Question

  • I'm needing to draw multiple lines, each one a set of data, and each successive line incrementing through a color gradient.

    Red remains fixed at 255. Blue remains fixed at 0. Green increments from 0 to 255. Green appears to increment properly, but the result is all lines draw Red. I'm not getting the expected gradient. Any assistance is appreciated.

    ' A gradient from Red to Yellow only needs to modify GREEN from 0 to 255.
    ' Find the gradient increment
    Dim GradInc As Single = TraceList(0).P1dB_Count / 255
    
    ' Loop through X_Index, Y Data
    For DataSet = 0 To TraceList(0).P1dB_Count - 1
    
      Dim G As New Pen(Color.FromArgb(255, CInt(DataSet * GradInc), 0))
    
      For y As Integer = 0 To DataPoints - 2
    
        ' Calculate X-Y values
    
        X1 = y * (921 / (DataPoints - 1))
        Y1 = 531 - ((TraceList(0).Y_PwrOut(DataSet, y) * TraceList(0).ScaleY) + Offset)
    
        X2 = (y + 1) * (921 / (DataPoints - 1))
        Y2 = 531 - ((TraceList(0).Y_PwrOut(DataSet, y + 1) * TraceList(0).ScaleY) + Offset)
    
        PNADisplayGfx.DrawLine(G, X1, Y1, X2, Y2)
    
      Next
    
      G.Dispose()
    
    Next

    Tuesday, November 19, 2019 5:00 PM

Answers

  • Turns out the gradient does work. The current code did not make it evident. The correction is:

    Dim GradInc As Single = TraceList(0).P1dB_Count / 255

    The GradInc value was so small it wasn't producing a noticeable gradient. My bad.

    • Marked as answer by dw80916 Tuesday, November 19, 2019 6:29 PM
    Tuesday, November 19, 2019 6:29 PM

All replies

  • Hi

    A quick glance through yourcode.

    Could it be:

      Dim G As New Pen(Color.FromArgb(255, 255, CInt(DataSet * GradInc), 0))


    Regards Les, Livingston, Scotland

    Tuesday, November 19, 2019 5:28 PM
  • I noticed that in my code when I added 'G' to the watch list, and made the change you indicated here. The results are the same.
    Tuesday, November 19, 2019 5:37 PM
  • Hi

    OK then, are you losing the value of the 'GradInc' variable by Disdposing G


    Regards Les, Livingston, Scotland

    Tuesday, November 19, 2019 5:49 PM
  • Here's the state of G several iterations into the loop. You can see the GREEN value is incrementing as expected.

    I've also tried G with and without dispose with same results. I'm not sure the significance of [R] being bracketed?

    Tuesday, November 19, 2019 5:57 PM
  • Noticed I'm passing values as Integer instead of Byte. Will make changes and update.
    Tuesday, November 19, 2019 6:01 PM
  • Turns out the gradient does work. The current code did not make it evident. The correction is:

    Dim GradInc As Single = TraceList(0).P1dB_Count / 255

    The GradInc value was so small it wasn't producing a noticeable gradient. My bad.

    • Marked as answer by dw80916 Tuesday, November 19, 2019 6:29 PM
    Tuesday, November 19, 2019 6:29 PM