コガネブログ

平日更新を目標に Unity や C#、Visual Studio、ReSharper などのゲーム開発アレコレを書いていきます

【Unity】指定された 16 進数を Color 型に変換する関数

ソースコード

using System.IO;
using UnityEngine;

public static class ColorUtils
{
    /// <summary>
    /// 指定された 16 進数を色に変換します
    /// </summary>
    /// <example>
    /// <code>
    /// // RGBA(1.000, 0.502, 0.000, 1.000)
    /// ColorUtils.ToARGB( 0xFFFF8000 )
    /// </code>
    /// </example>
    public static Color ToARGB( uint val )
    {
        var inv = 1f / 255f;
        var c = Color.black;
        c.a = inv * ( ( val >> 24 ) & 0xFF );
        c.r = inv * ( ( val >> 16 ) & 0xFF );
        c.g = inv * ( ( val >> 8  ) & 0xFF );
        c.b = inv * ( val & 0xFF );
        return c;
    }
}

関連記事