Hi there, How can i make a C# Color lighter or darker? also having the ability to check if a color is darker or lighter. I tried all the codes online but they never seem to work. Please advise
Depending on what you specifically want to do with the colors, one simple approach is just to multiply the color by a value higher than 1 (lighter) or lower than one (darker). For example: Code (CSharp): var red = Color.red; var lightRed = red * 1.5f; var darkRed = red * 0.5f; // Correct alpha. lightRed.a = 1; darkRed.a = 1; Note that this affects the alpha channel, which is why I've reset it to 1. Alternately you can create a new color with: Code (CSharp): var lightRed = new Color(red.r * 1.5f, red.g * 1.5f, red.b * 1.5f);
And checking darker/lighter you'd usually do by turning them into a greyscale value and then compare them.
Color.grayscale then if the grayscale value is lower than what you consider dark (will always be between 0 and 1), you lighten it.
The 'correct' way to do it is to convert your RGB Color to HSV (Hue,Saturation,Brightness/Value) using https://docs.unity3d.com/ScriptReference/Color.RGBToHSV.html You can then modify the saturation to make it more or less washed out or change the brightness level. Once you're done use https://docs.unity3d.com/ScriptReference/Color.HSVToRGB.html to convert it back again to RGB. You can also compare the relative brightness of 2 Colours by using the HSV 'Value'.
Color.Lerp(originalColor, Color.black, .5f); To darken by 50% Color.Lerp(originalColor, Color.white, .5f); To lighten by 50%