Winston Posted April 3, 2003 Posted April 3, 2003 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? Quote
*Experts* Nerseus Posted April 3, 2003 *Experts* Posted April 3, 2003 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 Quote "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
*Gurus* divil Posted April 3, 2003 *Gurus* Posted April 3, 2003 You can use the ControlPaint.Light function to increase a colour's brighness by a given factor. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Winston Posted April 4, 2003 Author Posted April 4, 2003 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? Quote
*Experts* Volte Posted April 4, 2003 *Experts* Posted April 4, 2003 Dim newColor As Color = ControlPaint.Light(Color.Red, 0.5) Lightens the color (red) by 50%. Then newColor contains the light red. Quote
Winston Posted April 4, 2003 Author Posted April 4, 2003 hmm its not quite the way i want to achieve it is it possible to control the lumniation of the color? Quote
*Gurus* divil Posted April 4, 2003 *Gurus* Posted April 4, 2003 No. The framework has a class for modifying the hue/luminance/saturation of colours, but in their infinite wisdom Microsoft made it private. You'll have find a third-party way to do this or write your own. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Leaders Squirm Posted April 4, 2003 Leaders Posted April 4, 2003 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. Quote Search the forums | Still IRCing | Be nice
Winston Posted April 5, 2003 Author Posted April 5, 2003 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 Quote
Leaders Squirm Posted April 6, 2003 Leaders Posted April 6, 2003 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. Quote Search the forums | Still IRCing | Be nice
Winston Posted April 6, 2003 Author Posted April 6, 2003 well thanks to all who had helped me but i found my resolution and it was just plain stupid and simple as well gees!! thanks all Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.