I need help doing a diagonal gradient

I can do a diagonal gradient from left to right but can’t get a right to left. I’m sure its simple but I have struggled for an hour trying to figure it out. Please have a look at the code below. :slight_smile:

[code]Dim startColor As Color = &c33333300
Dim endColor As Color = &c80808000

Dim p as New Picture(5, 5)
Dim samt, eamt As Double

For i As Integer = 0 To p.Height
samt = 1 - (i / p.Height)
eamt = i / p.Height
p.Graphics.ForeColor = RGB((startColor.Red * samt) + (endColor.Red * eamt), _
(startColor.Green *samt) + (endColor.Green * eamt), _
(startColor.Blue * samt) + (endColor.Blue * eamt))

if not doReverse then
  p.Graphics.DrawLine(i, 0, 0, i)//<<<--- this is working the way I expect it to.
else
  p.Graphics.DrawLine(p.Width , i, p.Width-i, p.Height-i)//<<<--- I can't get this to do a reverse version of the diagonal gradient here. 
  
end if

Next[/code]

5 x 5? why bother drawing it? why not just make a PNG or JPG and use that?

but here is the code

p.graphics.DrawLine(p.graphics.Width , i, i, p.graphics.Height)

Also… it (true/false both) do not FILL the rectangle… just a 45deg slice… but I assume that is what you want

Thank you for the code Dave. Correct. I do not want to fill the rectangle.

I never considered using a png or jpg. I have always drawn my own shadows and such in code.

Do a rectangular gradient, then rotate it. MBS and Studio Stable Graphics have fast, high quality, cross platform free rotation functions.

On the Mac you can use NSGradient or CGGradient (declares should be done in the MacOSLib). If you’ve never used either CG or NS before, it can be a little daunting, however you can do gradients at any angle and incredibly fast. Even circular gradients.

I think this chunk of code will do the trick. It draws the shadow in the upper right-hand corner:

[code] dim g as graphics

g=p.graphics

For i As Integer = 0 To g.Height
samt = 1 - (i / g.Height)
eamt = i / g.Height
g.ForeColor = RGB((startColor.Red * samt) + (endColor.Red * eamt), _
(startColor.Green *samt) + (endColor.Green * eamt), _
(startColor.Blue * samt) + (endColor.Blue * eamt))

if not doReverse then
  g.DrawLine(i, 0, 0, i)//<<<--- this is working the way I expect it to.
else
  g.DrawLine(g.Width-i, 0, g.width, i)//<<<--- I can't get this to do a reverse version of the diagonal gradient here.
end if

Next
[/code]

Note that I declared a Graphics object and assigned p.graphics to it. This makes the code easier to read and more flexible; you can take this code and drop it into any Paint event and have it work. It might also be marginally faster this way (very, very marginally).