Would it be actually changing the hue or sat of the color?

Winston

Well-known member
Joined
Jan 25, 2003
Messages
266
Location
Sydney, Australia
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?
 
Im not sure of the direct correlation between RGB and the Hue/Saturation values, but heres a trick Ive 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:
C#:
// 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, its not used (for the most part) for coloring controls. Dont try tweaking the alpha hoping to get a brighter or darker color.

-Nerseus
 
You can use the ControlPaint.Light function to increase a colours brighness by a given factor.
 
Code:
Dim newColor As Color = ControlPaint.Light(Color.Red, 0.5)
Lightens the color (red) by 50%. Then newColor contains the light
red.
 
No. The framework has a class for modifying the hue/luminance/saturation of colours, but in their infinite wisdom Microsoft made it private. Youll have find a third-party way to do this or write your own.
 
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:

Code:
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.
 
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




PHP:
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
 
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.
 
Back
Top