- Home /
How to Get / Set HDR Color intensity
We have an EmissionColor that is HDR enabled, and we'd like to adjust its intensity value directly through C# (I'd prefer not to touch our post-processing shaders). I feel this should be an easy solution, but I haven't found an answer for this specific question (this question is the closest one, but I'm not looking to swap colors- just adjust a color's intensity value). Here's sample code:
Color colorToDisplay = targetMat.GetColor("_EmissionColor");
/*I want to do something like this:
if(colorToDisplay.Intensity > 0.5)
{
colorToDisplay.Intensity = 1;
}*/
targetMat.SetColor("_EmissionColor", colorToDisplay);
I could use a few Color.Lerps, since I'm getting the intensity value to transition that way, but it's clunky for what I'd like to do. I could also follow bgolus's response in this thread, but I'm curious to know if there's a simpler way. Thank you for your time!
Answer by hugodigio · Jan 31, 2020 at 10:30 AM
I search a lot before found the solution, I don't found it on documentation, but finally, it was simple. I found the operation done by colorPicker by analyzing values changes in RGB mode when we change intensity parameter. The operation is just 2^intensity
. There is the solution:
float factor = Mathf.pow(2,intensity);
Color color = new Color(red*factor,green*factor,bleue*factor);
Answer by Pangamini · Nov 14, 2019 at 01:55 PM
Color struct consist of 4 floats for each channel. To create a higher intensity color, simply set its values to something higher than 1. Intensity exists in the color picker editor, but it's probably just an editor value that multiplies all channels by it (that's my guess). So, define the 'intensity' first. Let's say it's the average of rgb channels
var intensity = (color.r + color.g + color.b) / 3f;
then, if you want to set an intensity to a specific value, find out how much you need to increase the intensity of each channel. If you want to 'normalize' the intensity (set it to 1) you'd do something like
var factor = 1f / intensity;
where factor is the value you will multiply the color's rgb by:
color = new Color(color.r * factor, color.g*factor, color.b * factor, color.a);
Now, if your original color wasn't gray, one or more channels will be higher than 1 (which is the top LDR value)
Answer by bbsuuo · Oct 19, 2020 at 08:29 AM
this decompose function is unity source code ,so you can try it ,
private const byte k_MaxByteForOverexposedColor = 191;
public static void DecomposeHdrColor(Color linearColorHdr, out Color32 baseLinearColor, out float exposure)
{
baseLinearColor = linearColorHdr;
var maxColorComponent = linearColorHdr.maxColorComponent;
// replicate Photoshops's decomposition behaviour
if (maxColorComponent == 0f || maxColorComponent <= 1f && maxColorComponent >= 1 / 255f)
{
exposure = 0f;
baseLinearColor.r = (byte)Mathf.RoundToInt(linearColorHdr.r * 255f);
baseLinearColor.g = (byte)Mathf.RoundToInt(linearColorHdr.g * 255f);
baseLinearColor.b = (byte)Mathf.RoundToInt(linearColorHdr.b * 255f);
}
else
{
// calibrate exposure to the max float color component
var scaleFactor = k_MaxByteForOverexposedColor / maxColorComponent;
exposure = Mathf.Log(255f / scaleFactor) / Mathf.Log(2f);
// maintain maximal integrity of byte values to prevent off-by-one errors when scaling up a color one component at a time
baseLinearColor.r = Math.Min(k_MaxByteForOverexposedColor, (byte)Mathf.CeilToInt(scaleFactor * linearColorHdr.r));
baseLinearColor.g = Math.Min(k_MaxByteForOverexposedColor, (byte)Mathf.CeilToInt(scaleFactor * linearColorHdr.g));
baseLinearColor.b = Math.Min(k_MaxByteForOverexposedColor, (byte)Mathf.CeilToInt(scaleFactor * linearColorHdr.b));
}
}
Answer by sterneck_moritz · Mar 31, 2021 at 03:16 PM
Wow, it took me a while to get here...
Combining the solution from the other answers:
private const byte k_MaxByteForOverexposedColor = 191; //internal Unity const
Color _emissionColor = GetComponent<Renderer>().material.GetColor("_EmissionColor");
var maxColorComponent = _emissionColor.maxColorComponent;
var scaleFactor = k_MaxByteForOverexposedColor / maxColorComponent;
float intensity = Mathf.Log(255f / scaleFactor) / Mathf.Log(2f);
This should give you the exact Intensity value from the HDR Color picker.