Unity Trying to get the Hue from a get Pixel using Color.RBGtoHSV but H always returns 0

I created a Unity Game Object and added a script

public Texture2D TheTexture;

void Awake()
{

    float H, S, V;
    Color c = TheTexture.GetPixel(4, 4);
    Color.RGBToHSV(c, out H, out S, out V);
    Debug.Log("H: " + H + " S: " + S + " V: " + V);
    Debug.Log(c);
}

The Texture is a 10x10 bmp where the entire image is a solid blue surface RGB 0,0,255

When I run this, the H comes up 0. The C comes up accurately, so I am reading in the image correctly, I believe.

Any idea what I am doing wrong. Ultimately I am just trying to get the hue of a color from a bitmap

1 answer

  • answered 2020-03-31 11:38 derHugo

    Well, in the code you posted you never change any of H, S and V ...

    you could use

    Color.RGBToHSV

    void Start()
    {
        Color c = TheTexture.GetPixel(4, 4);
    
        Color.RGBToHSV(c, out var H, out var S, out var V);
        Debug.Log("H: " + H + " S: " + S + " V: " + V);
    }
    

    I saw you added it in your question. But I can't confirm it. I suspect your pixel has the wrong color.

    For

    void Start()
    {
        Color c = new Color(0, 0, 1, 1);
    
        Color.RGBToHSV(c, out var H, out var S, out var V);
    
        Debug.Log(c);
        Debug.Log("H: " + H + " S: " + S + " V: " + V);
    }
    

    I get the correct output:

    RGBA(0.000, 0.000, 1.000, 1.000)

    H:0.6666667 S:1 V:1


    A Hue of 0 would actually be red ...