【Unity】16進数からColorに変換

DEVELOP, Unity

public static class ColorHelper
{
    public static Color HexToColor( uint color ) 
    {
        float r,g,b,a;
        var inv = 1.0f / 255.0f;
        if( color > 0xffffff )
        {
            r = ( ( color >> 24 ) & 0xff ) * inv;
            g = ( ( color >> 16 ) & 0xff ) * inv;
            b = ( ( color >> 8  ) & 0xff ) * inv;
            a = ( ( color       ) & 0xff ) * inv;
        }
        else
        {
            r = ( ( color >> 16 ) & 0xff ) * inv;
            g = ( ( color >> 8  ) & 0xff ) * inv;
            b = ( ( color       ) & 0xff ) * inv;
            a = 1.0f;
        }
        return new Color( r, g, b, a );
    }
}

Posted by kazupon