コード例 #1
0
ファイル: HexUtil.cs プロジェクト: bigstupidx/SoulAvenger
    /// <summary>
    /// Convert an hexadecimal color representation RRGGBBAA into a Color value
    /// </summary>
    /// <param name="value">The hexadecimal color</param>
    /// <param name="color">The corresponding color</param>
    /// <returns>Whether the returned color is valid or not</returns>
    public static bool HexToColor(string value, out Color color)
    {
        if (value.Length != 8)
        {
            color = Color.white;
            return(false);
        }

        string rText = value.Substring(0, 2);
        string gText = value.Substring(2, 2);
        string bText = value.Substring(4, 2);
        string aText = value.Substring(6, 2);

        float r = HexUtil.HexToInt(rText) / 255.0f;
        float g = HexUtil.HexToInt(gText) / 255.0f;
        float b = HexUtil.HexToInt(bText) / 255.0f;
        float a = HexUtil.HexToInt(aText) / 255.0f;

        if (r < 0 || g < 0 || b < 0 || a < 0)
        {
            color = Color.white;
            return(false);
        }

        color = new Color(r, g, b, a);
        return(true);
    }