Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Well guys in my program i have panels

 

the panels have paint events

 

and my friend gave me this code it draws linear gradients using two colors

i really love it, but i have one certain pane in my program in which i want the colours to be a bit lighter

 

so say the user has selected the colors

 

red and white

 

so all the panes will have red and fading into white

 

 

but in this certain pane i dont quite want that particular darkness of the red, i want that red to be tone down to be a littl elighter

 

would i have to control that particular colors hue and saturation

 

if yes how would i do so?

  • *Experts*
Posted

I'm not sure of the direct correlation between RGB and the Hue/Saturation values, but here's a trick I've used in the past with good results.

 

From each color (say red, which is r=255,g=0,b=0), increase all 3 values by a certain amount, with a cap of 255. For example:

// untested, but hopefully working :)
int increaseVal = 20; // Arbitrary value, depending on how "light" you want to make your new color
Color r = Color.Red;
Color new = r;
new.r = Math.Min(255, new.r + increaseVal);
new.g = Math.Min(255, new.g + increaseVal);
new.b = Math.Min(255, new.b + increaseVal);

 

While the Color type supports an alpha, it's not used (for the most part) for coloring controls. Don't try tweaking the alpha hoping to get a brighter or darker color.

 

-Nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted
You can use the ControlPaint.Light function to increase a colour's brighness by a given factor.

 

 

how exactly would u apply this setting in to use?

  • *Experts*
Posted

Dim newColor As Color = ControlPaint.Light(Color.Red, 0.5)

Lightens the color (red) by 50%. Then newColor contains the light

red.

  • Leaders
Posted

Here is a function I cooked up which creates a Color from HSL values in the range 0-239. It seems to work exactly as the one in the choose colour dialog does:

 

Private Function ColorHSL(ByVal hue As Integer, ByVal sat As Integer, ByVal lum As Integer) As Color

   Dim r, g, b As Double
   Dim dblhue, dbllum, dblsat As Double
   Dim temp As Double

   hue = hue Mod 240
   sat = sat Mod 240
   lum = lum Mod 240

   dblhue = Convert.ToDouble(hue)
   dblsat = Convert.ToDouble(sat)
   dbllum = Convert.ToDouble(lum)

   'Hue
   If hue < 80 Or hue > 160 Then
       If hue > 160 Then temp = 240 - dblhue Else temp = dblhue
       If (temp > 40) Then temp = temp - 40 Else temp = 0
       r = 255 - (temp * 6.375)
   End If
   If hue < 160 Then
       temp = Math.Abs(dblhue - 80)
       If (temp > 40) Then temp = temp - 40 Else temp = 0
       g = 255 - (temp * 6.375)
   End If
   If hue > 80 Then
       temp = Math.Abs(dblhue - 160)
       If (temp > 40) Then temp = temp - 40 Else temp = 0
       b = 255 - (temp * 6.375)
   End If

   'Saturation
   r = r + ((127 - r) * (dblsat / 240))
   g = g + ((127 - g) * (dblsat / 240))
   b = b + ((127 - b) * (dblsat / 240))

   'Luminosity
   If lum > 120 Then
       r = r + ((255 - r) * ((dbllum - 120) / 120))
       g = g + ((255 - g) * ((dbllum - 120) / 120))
       b = b + ((255 - b) * ((dbllum - 120) / 120))
   ElseIf lum < 120 Then
       r = r * (dbllum / 120)
       g = g * (dbllum / 120)
       b = b * (dbllum / 120)
   End If

   Return Color.FromArgb(Convert.ToInt32(r), Convert.ToInt32(g), Convert.ToInt32(b))

End Function

 

Hope that helps.

Posted

how exactly would i apply this?

 

i mean all i want to do is control the lumniation of one color

 

 

 

this is the code i use to paint my panels

 

 

 

 

Dim g As Graphics = e.Graphics
       Dim tempRect As Rectangle = New Rectangle(0, 0, pnlSplash.Width, pnlSplash.Height)
       Dim tempBrush As System.Drawing.Drawing2D.LinearGradientBrush = _
       New System.Drawing.Drawing2D.LinearGradientBrush(tempRect, Schedular.Settings.SchemeColor1, Schedular.Settings.SchemeColor2, System.Drawing.Drawing2D.LinearGradientMode.Vertical)
       g.FillRectangle(tempBrush, tempRect)

 

 

Schedular.settings.schemecolor1

 

is the color thats taken from the registry

 

and i want to control the lumination of it

 

how would i use ur function to achieve this

 

 

thanks for ur help

  • Leaders
Posted

I see what you want to do, but its really not very simple. HSL and RGB are two very different systems. HSL is easier for users and RGB is more suited to programs. It isnt too hard to convert from HSL to RGB but converting the other way is more complex.

 

If you just want to alter the luminosity of a color, you need to get the individual RGB components and multiply them with a scaling factor, as I have done in that final section of my code.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...