using UnityEngine;
using UVC.Log;

namespace UVC.Util
{
    public static class ColorUtil
    {
        /// <summary>
        /// hex code to color #FFFFFF
        /// </summary>
        /// <param name="hexCode">#rrggbb</param>
        /// <returns>Color °΄ΓΌ</returns>
        /// <example>
        /// // Example usage:
        /// Color color = ColorUtil.FromHex("#FF5733");
        /// Debug.Log($"Color: {color}");
        /// </example>
        public static Color FromHex(string hexCode)
        {
            Color color;
            if (ColorUtility.TryParseHtmlString(hexCode, out color))
            {
                return color;
            }

            ULog.Error($"[UnityExtension::FromHex]invalid hex code: {hexCode}", new System.Exception("$[UnityExtension::FromHex]invalid hex code: {hexCode}"));
            return Color.white;
        }

        /// <summary>
        /// hex code to color with alpha #FFFFFF
        /// </summary>
        /// <param name="hexCode">#rrggbb</param>
        /// <param name="alpha">0.0~1.0</param>
        /// <returns>Color °΄ΓΌ</returns>
        /// <example>
        /// // Example usage:
        /// Color colorWithAlpha = ColorUtil.FromHex("#FF5733", 0.5f);
        /// Debug.Log($"Color with alpha: {colorWithAlpha}");
        /// </example>
        public static Color FromHex(string hexCode, float alpha)
        {
            Color color;
            if (ColorUtility.TryParseHtmlString(hexCode, out color))
            {
                color.a = alpha;
                return color;
            }

            ULog.Error($"[UnityExtension::HexColor]invalid hex code: {hexCode}", new System.Exception($"[UnityExtension::HexColor]invalid hex code: {hexCode}"));
            color = Color.white;
            color.a = alpha;
            return color;
        }
    }
}
