   Fade RGB value to black or white
  I am wanting to fade a RGB value to white or black and the only way I know how would be to convert it to HSL, adjust the light value and turn it back into RGB again. The only problem is that could turn out to be rather expensive when it has to be done for every pixel.I found this site which tells you the algorithm for converting between RGB and HSL:http://www.easyrgb.com/math.php?MATH=M18#text18Since I am only interested in the light value I was wondering if it were possible to just convert the RGB values into L and then back again without needing to convert it into H and S values.So this code will convert RGB value into L value var_R = ( R / 255 ) //Where RGB values = 0 ÷ 255var_G = ( G / 255 )var_B = ( B / 255 )var_Min = min( var_R, var_G, var_B )    //Min. value of RGBvar_Max = max( var_R, var_G, var_B )    //Max. value of RGBL = ( var_Max + var_Min ) / 2But how would I then convert that L value back into RGB without knowing the H and S values?I tried just doing a linear fade between the colour I have (e.g. Purple) and Black but it doesn't fade straight to black, instead it goes from Purple -> Dark Green -> Black.Otherwise any other ideas would be great.Thanks   
  I'd try interpolating between your colour and either white or black.For any f between 0.0 and 1.0, you can interpolate between a and b like:fade = (f*a + (1.0-f)*b)/2.0You can do that for each of your colour components (interpolate with 1.0 or 255 for white, and 0 or 0.0 for black). If all components have the same factor, that should do well (no funny colours).This should be quite fast and give you just what you want.If you move it to a shader later, there are even builtin functions for that (mix() in GLSL or lerp() in Cg/HLSL).   ;  Yeah, it's not perfectly correct, technically speaking, but simply changing the RGB values themselves works well enough and is fast. Just move each color component towards the target color by some value such that they arrive at the target color at the same time. Do this over several frames.Be careful of very slow and/or very small fades, because you'll need additional precision to pull them off smoothly. 16 bits of precision is usually enough for 8 bit components, so I simply pre-scale the components and adjust the formula as necessary.   ;  Hi and thanks for your replies.I thought I would try and implement the algorithm given on the web page for HSL to RGB conversion but as ravyne said it required more precision. Annoyingly though I was still getting shades of green despite only changing the Light value.I finally realised that the algorithm that I use to convert my gray scale image to a shade of colour was returning values larger than 1.0. So what I thought was white was actually an extreme version of a colour and as I brought it back down to black it had to go down from white past its original colour and finish at black. After clamping the values I was able to fix this and do a simple linear interpolation instead of going through HSL.I attempted samoth's linear interpolation:fade = (f*a + (1.0-f)*b)/2.0I am not sure why you have a division of 2.0 in there since it means I will only get to half the colour that I want it to get to.Anyway without the division it worked finefloat f = smoothstep(0.8, 0.5, len);vec3 a = colorRGB;vec3 b = blackPart;color = (f*a + (1.0-f)*b);Since black is 0.0 I thought I would simply make it thisvec3 change = colorRGB * smoothstep(0.8, 0.5, len);color = colorRGB - change ;It now works great.Thanks again for your help.   ;Quote:Original post by 4fingersI am not sure why you have a division of 2.0 in thereStupid late-night mistake of mine, sorry. Mixed up average and weighted average due to lack of concentration (doing both).You posted some GLSL code there. In that case, you will want to use the mix() function which does the same thing as explicitely writing out the math, but gives the compiler a strong hint to what you're attempting. It also very likely uses an opcode (LRP) that's specifically made for that purpose (---> better performance).   ;Quote:Original post by samothYou posted some GLSL code there. In that case, you will want to use the mix() function which does the same thing as explicitely writing out the math, but gives the compiler a strong hint to what you're attempting. It also very likely uses an opcode (LRP) that's specifically made for that purpose (---> better performance).Your absolutely rightcolor = mix(colorRGB, blackPart, smoothstep(0.8, 0.5, len));Achieves the same thing using just one line.Thanks again.   ;Quote:Original post by 4fingersYour absolutely rightcolor = mix(colorRGB, blackPart, smoothstep(0.8, 0.5, len));Achieves the same thing using just one line.Thanks again.Be careful with that because you are relying on undefined behavior with that smoothstep call.  Here is the description of smoothstep from the GLSL Specs...Quote:genType smoothstep(genType edge0, genType edge1, genType x)genType smoothstep(float edge0, float edge1, genType x)Returns 0.0 if x <= edge0 and 1.0 if x >= edge1 andperforms smooth Hermite interpolation between 0 and 1when edge0 < x < edge1. This is useful in cases whereyou would want a threshold function with a smoothtransition. This is equivalent to:genType t;t = clamp ((x – edge0) / (edge1 – edge0), 0, 1);return t * t * (3 – 2 * t);Results are undefined if edge0 >= edge1.