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.
[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
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).