I want to change a color based on temperature

mpooley

Active member
Joined
Jan 4, 2009
Messages
32
Programming Experience
Beginner
HI
I am building a temperature sensor network and want to graphically show tempperatures with different colors.

So I need a nice blend from blue green yellow reds as the temps rise.

To be honest I havn't got a clue apart fom hard coding it which i would prefer not to do.

Has any one done this or offer any advice please?

Mike
 
Give it a go with this VB Helper: HowTo: Make a LinearGradientBrush that blends three colors in VB .NET
Most likely you will either draw the full 'scale' of colors and indicate the currect temp with some marker, or only draw the part of the 'scale' that indicates the current level - 'scale' here being the rectangle to draw.

Thanks John that is very interesting. I have altered the code to give me a nice blend of colours from a cool blue to a hot red - its just right .
But i still cant see a way of selecting from that range, a particular colour? which is what I want .

I will see what i can dig up unless you have any more advice lol

Thanks again

Mike:)
 
But i still cant see a way of selecting from that range, a particular colour? which is what I want .
Don't understand :confused:
 
Don't understand :confused:

Sorry:D

depending on the temperature of a room I want a particular color to represent that.

I want it in the range defined by the gradient fill .

So I will divide the gradient into say 60 parts and select a colour in that part defined by an integer temperature .
Does that make sense?

I have decided to use "getpixel" to return the colour but that only works on bitmaps so i need to convert the rectangle into a bit map and it should be easy ! LoL

just trying to do that now.
It's ages since i've done any programming so am a bit rusty.

Mike
 
Does that make sense?
Not really, usually the gradients represent a scale similar (and complimentary) to numbers f.ex 0-30. When temperature changes the scale does not, it just represent a value on that scale. So my interpretation of drawing this color scale would be as suggested that you either draw the full scale always and indicate the current temperature value on that scale (with a line?), or perhaps only draw the part of the scale indicates the current level. It is the current temperature value that determines where on the scale you indicate the level.
depending on the temperature of a room I want a particular color to represent that.
The gradient brush rectangle represent the full scale. The break points of the color blends is set with values between 0.0 and 1.0 (percentage). While FillRectangle represent how much of the scale you want to draw.
 
ok but i have achieved exactly what i want thanks.
I basicaly am drawing a gradient into a bit map and then using getpixel to get a particular colour along the gradient
code has not been polished just for test purposes :)
Dim myBitmap As Bitmap
Dim G As Object
myBitmap = New Bitmap(450, 40, PixelFormat.Format32bppArgb)
G = Graphics.FromImage(myBitmap)



Dim width_of_form As Integer = Me.ClientSize.Width - 10
Dim the_brush As LinearGradientBrush
the_brush = New LinearGradientBrush(New Point(0, 0), New Point(width_of_form, 0), Color.Red, Color.Blue)
the_brush.WrapMode = WrapMode.Tile

' Define the colors.
Dim color_blend As New ColorBlend
color_blend.Colors = New Color() {Color.Red, Color.Orange, Color.Yellow, Color.GreenYellow, Color.Blue}
color_blend.Positions = New Single() {0.0, 0.3, 0.5, 0.7, 1.0}
the_brush.InterpolationColors = color_blend


the_brush.GammaCorrection = True


'Height = 80

G.FillRectangle(the_brush, New Rectangle(0, 0, 450, 40))

Me.picturebox.Height = 40

Me.picturebox.Width = 450
Me.Width = 450
Me.picturebox.Image = myBitmap
Dim pos As System.Drawing.Point
picturebox.Location() = (pos)

PictureBox1.BorderStyle = BorderStyle.FixedSingle
PictureBox1.Show()
'this is just a test
'x is the temperature times a multiplier ?
Dim x As Integer = 325
Dim y As Integer = 19

Try
Dim colour As Color = myBitmap.GetPixel(x, y)
PictureBox1.BackColor = colour
Catch ex As Exception
Stop
End Try
 
Ok, I see, you want to use the color of the gradient at a particular temperature level as BackColor (indicator) for the Picturebox. That is plain and simple, but I didn't understand that from your previous explanations.
'x is the temperature times a multiplier ?
Yes, if the scale is 0-30 vertical and width 100, and temp is 10, then you get color at 10/30*100=33.
 
Ok, I see, you want to use the color of the gradient at a particular temperature level as BackColor (indicator) for the Picturebox. That is plain and simple, but I didn't understand that from your previous explanations.

Yes, if the scale is 0-30 vertical and width 100, and temp is 10, then you get color at 10/30*100=33.

yeah sorry - I didnt phrase it very well.

But i knew what i meant lol

Thanks

Mike
 
Back
Top