- Home /
Modify Sprite HSV Values
I am trying to modify the HSV values of my ingame sprites via this code:
float greenLv = 0.1f;
//Access each childrenderer in the game
foreach (Renderer childRend in rend) {
Debug.Log (childRend.name);
currColor = childRend.material.color;
Debug.Log ("currentColor" + currColor);
Color.RGBToHSV (currColor, out h, out s, out v);
Debug.Log (" child HSV values colors " + h + "," + s + ","+ v);
currColor = Color.HSVToRGB(greenLv,greenLv,greenLv);
childRend.material.color = currColor;
}
For some reason, the HSV values I get are always (0,0,1) - which should mean the color black? Even though I know for sure the sprite isn't black. Is it getting the first pixel it encounters? How can I possible edit the overall tint of a sprite?
Thank you in advance!
Answer by yummy81 · Feb 24, 2018 at 10:17 AM
Let's take a closer look at your code. Inside foreach loop you assign each individual material's color to the "currColor" variable. Then, you translate it into HSV values. As a result, color is stored in h, s and v variables now. Next, you want to reverse the operation converting HSV values back to RGB values and assign the result to childRend.material.color, but instead of doing this:
currColor = Color.HSVToRGB(h, s, v);
you are doing that:
currColor = Color.HSVToRGB(greenLv,greenLv,greenLv);
That simply means that you do not assign color stored in h, s, v floats, but hardcoded color (0.1f, 0.1f, 0.1f). That's because greenLv = 0.1f. And, that's why you see black color (of course it's not entirely black (0.0f), but almost black (0.1f)). You also ask whether HSV values (0, 0, 1) means black color - no, it's white, but because you assign greenLv float, you see black color. I'm not sure what your final goal is, but I created a small script which allows to manipulate H, S, V floats and see how colors of each child sprite change. Below, there is how my experimental scene looks like, and the script. Put the script on HonestKun gameobject. Remember that each child sprite has its own individual material: Circle gameobject has CircleMaterial, Square has SquareMaterial, and Triangle has TriangleMaterial.
using UnityEngine;
public class HonestKun : MonoBehaviour
{
[Range(0, 1)] public float h = 0f, s = 0f, v = 1f;
private Renderer[] rend;
private void Awake()
{
rend = GetComponentsInChildren<Renderer>();
}
private void Update()
{
Color color = Color.HSVToRGB(h, s, v);
foreach (Renderer r in rend)
{
r.material.color = color;
}
}
}