Duplicate
How do I adjust the brightness of a color?
How do I determine darker or lighter color variant of a given color?
Programmatically Lighten a Color
Say I have
var c = Color.Red;
Now I want to create a new Color that is lighter or darker than that color. How can I do that without too much hassle?
-
Using HSI converter library(search google). And then, adjust I channel for lighter/darker color.
-
ControlPaint.Light .Dark .DarkDark, etc.
Color lightRed = ControlPaint.Light( Color.Red );schnaader : corrected your typo, although a ControlPain class would be funny :)Paul Alexander : hehe...if you've every done any you know how true that is.Svish : ControlPaint.Light and .Dark are perfect :D Especially with the extra percentage float parameter thingy.Soul_Master : Today, I realize that .Net framework is very huge size. Because I don't know about ControlPaint class before. But I use only 3-party image processing(like AForge.net). Thanks. -
Take a look at the ControlPaint class:
-
Here's some javascript code I use for lightening/darkening a given colour. You could use it as a base for an equivalent C# function
It works by calculating a distance from pure white of each of the RGB components and then adjusts this distance by the provided factor. The new distance is used to calculate the new colour. A factor of between 0 and 1 darkens, a factor higher than 1 lightens
function Darken( hexColor, factor ) { if ( factor < 0 ) factor = 0; var c = hexColor; if ( c.substr(0,1) == "#" ) { c = c.substring(1); } if ( c.length == 3 || c.length == 6 ) { var i = c.length / 3; var f; // the relative distance from white var r = parseInt( c.substr(0, i ), 16 ); f = ( factor * r / (256-r) ); r = Math.floor((256 * f) / (f+1)); r = r.toString(16); if ( r.length == 1 ) r = "0" + r; var g = parseInt( c.substr(i, i), 16); f = ( factor * g / (256-g) ); g = Math.floor((256 * f) / (f+1)); g = g.toString(16); if ( g.length == 1 ) g = "0" + g; var b = parseInt( c.substr( 2*i, i),16 ); f = ( factor * b / (256-b) ); b = Math.floor((256 * f) / (f+1)); b = b.toString(16); if ( b.length == 1 ) b = "0" + b; c = r+g+b; } return "#" + c; } -
You can also do this using a
Lerpfunction. There's one in XNA, but it's easy to write yourself.See my answer to this similar question for a C# implementation.
The function lets you do this:
// make red 50% lighter: Color.Red.Lerp( Color.White, 0.5 ); // make red 75% darker: Color.Red.Lerp( Color.Black, 0.75 ); // make white 10% bluer: Color.White.Lerp( Color.Blue, 0.1 );
0 comments:
Post a Comment