-
Aug 26th, 2014, 08:57 AM
#1
Thread Starter
New Member
VB Studio 2005
I am trying to create a function to return a color based on a value and then draw a line that color. I need a specific color gradient for each value I send between 0 and 1500. No errors but the color never shows and the line never shows. If I force the line color to black, that works.
Code:
Public Class Form1
Public mypen As Pen
Public rcol As Integer = 100
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Function getrgb(ByVal cc As Integer) As Integer
Dim rt As Integer
If cc <= 255 Then
rt = RGB(255, cc, 0)
ElseIf cc < 511 Then
rt = RGB(255 - (cc - 255), 255, 0)
ElseIf cc < 766 Then
rt = RGB(0, 255, cc - 511)
ElseIf cc < 1022 Then
rt = RGB(0, 255 - (cc - 767), 255)
ElseIf cc < 1277 Then
rt = RGB(0, 0, 255 - (cc - 1022))
Else
rt = RGB(0, 0, 0)
End If
getrgb = rt
End Function
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Dim rr As Integer = getrgb(rcol)
mypen = New Pen(Color.FromArgb(rr))
e.Graphics.DrawLine(mypen, 0, 0, Me.Width, Me.Height) 'doesn't work
'e.Graphics.DrawLine(Pens.Black, 0, 0, Me.Width, Me.Height) 'works
mypen.Dispose()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Me.Refresh()
rcol += 1
If rcol > 1500 Then
rcol = 0
End If
End Sub
End Class
-
Aug 26th, 2014, 09:09 AM
#2
Re: VB Studio 2005
seems over complicated...
I'd change the function to return a color rather than an integer, then use that directly with the pen constructor:
vb.net Code:
Function GetColor(ByVal cc As Integer) As System. Dim rt As Integer If cc <= 255 Then rt = RGB(255, cc, 0) ElseIf cc < 511 Then rt = RGB(255 - (cc - 255), 255, 0) ElseIf cc < 766 Then rt = RGB(0, 255, cc - 511) ElseIf cc < 1022 Then rt = RGB(0, 255 - (cc - 767), 255) ElseIf cc < 1277 Then rt = RGB(0, 0, 255 - (cc - 1022)) Else rt = RGB(0, 0, 0) End If getrgb = rt End Function
Then
vb.net Code:
Using myPen As New Pen(GetColor(rr)) e.Graphics.DrawLine(myPen, 0, 0, Me.Width, Me.Height) 'doesn't work 'e.Graphics.DrawLine(Pens.Black, 0, 0, Me.Width, Me.Height) 'works End Using
BTW - there's no need for MyPen to be declared where it is... it's only going to be used in the Paint event, and you're disposing it each time. That's why I used the Using statement... it'll create the pen, then auto dispose it at the end.
-tg
-
Aug 26th, 2014, 10:14 AM
#3
Re: VB Studio 2005
using LinearGradientBrush to handle gradients.
Code:
Private Sub Form1_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Dim color As New Color
Dim m_Rnd As New Random
color = color.FromArgb(255, _
m_Rnd.Next(0, 255), _
m_Rnd.Next(0, 255), _
m_Rnd.Next(0, 255))
Using linGrBrush As New LinearGradientBrush(New Point(0, 10), New Point(200, 10), color.FromArgb(Getrgb(rcol)), color)
Using mypen As New Pen(linGrBrush, 2)
e.Graphics.DrawLine(mypen, 0, 0, Me.Width, Me.Height)
End Using
End Using
End Sub
KGC
-
Aug 26th, 2014, 02:17 PM
#4
Thread Starter
New Member
Re: VB Studio 2005
Thanks for the examples...
techgnome example still does not draw a line in any color.
KGComputers example makes a nice line with a gradient along the distance but I need the routine to return 1 color of the rainbow and draw the line that color.
-
Aug 26th, 2014, 02:34 PM
#5
Re: VB Studio 2005
In KGComputer's example, it's up to you as the programmer to make the color variable a similar color to the Getrgb(rcol) variable if you want the gradient line to be just one color variation.
-
Aug 26th, 2014, 03:01 PM
#6
Thread Starter
New Member
Re: VB Studio 2005
DOH!
The alpha component was set to 0.
Now it works.
Code:
Public Class Form1
Public rcol As Integer = 700
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Function getrgb(ByVal cc As Integer) As System.Drawing.Color
Dim returnValue As Color
Dim rt As Integer
If cc <= 255 Then
rt = RGB(255, cc, 0)
returnValue = Color.FromArgb(255, 255, cc, 0)
ElseIf cc < 511 Then
rt = RGB(255 - (cc - 255), 255, 0)
returnValue = Color.FromArgb(255, 255 - (cc - 255), 255, 0)
ElseIf cc < 767 Then
rt = RGB(0, 255, cc - 511)
returnValue = Color.FromArgb(255, 0, 255, cc - 511)
ElseIf cc < 1022 Then
rt = RGB(0, 255 - (cc - 767), 255)
returnValue = Color.FromArgb(255, 0, 255 - (cc - 767), 255)
ElseIf cc < 1277 Then
rt = RGB(0, 0, 255 - (cc - 1022))
returnValue = Color.FromArgb(255, 0, 0, 255 - (cc - 1022))
Else
rt = RGB(0, 0, 0)
returnValue = Color.FromArgb(255, 0, 0, 0)
End If
getrgb = returnValue
End Function
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Dim gc As Color = getrgb(rcol)
Using mypen As New Pen(gc, 2)
e.Graphics.DrawLine(mypen, 0, 0, Me.Width, Me.Height)
End Using
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Me.Refresh()
rcol += 1
If rcol > 1200 Then
rcol = 0
End If
End Sub
End Class
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|