use extension methods for hex conversion & support alpha colors
This commit is contained in:
parent
e09e1525c2
commit
a234d4a113
|
@ -273,9 +273,9 @@ namespace Celeste.Mod.AvaliSkin {
|
|||
// this will get (de)serialized from/into a yaml list
|
||||
[SettingIgnore]
|
||||
public IEnumerable<string> DashRGB {
|
||||
get => DashRGBColor.Select(c => ColorUtil.ColorToHex(c));
|
||||
get => DashRGBColor.Select(c => c.ToHexA());
|
||||
set {
|
||||
DashRGBColor = value.Select(c => ColorUtil.HexToColor(c)).ToList();
|
||||
DashRGBColor = value.Select(c => c.HexToColor()).ToList();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -285,8 +285,8 @@ namespace Celeste.Mod.AvaliSkin {
|
|||
|
||||
[SettingIgnore]
|
||||
public string LightBodyRGB {
|
||||
get => ColorUtil.ColorToHex(LightBodyRGBColor);
|
||||
set { LightBodyRGBColor = ColorUtil.HexToColor(value); }
|
||||
get => LightBodyRGBColor.ToHexA();
|
||||
set { LightBodyRGBColor = value.HexToColor(); }
|
||||
}
|
||||
|
||||
[SettingIgnore]
|
||||
|
@ -295,8 +295,8 @@ namespace Celeste.Mod.AvaliSkin {
|
|||
|
||||
[SettingIgnore]
|
||||
public string DarkBodyRGB {
|
||||
get => ColorUtil.ColorToHex(DarkBodyRGBColor);
|
||||
set { DarkBodyRGBColor = ColorUtil.HexToColor(value); }
|
||||
get => DarkBodyRGBColor.ToHexA();
|
||||
set { DarkBodyRGBColor = value.HexToColor(); }
|
||||
}
|
||||
|
||||
// Stores submenu items that are enabled/disabled when colormode is RGB
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.Xna.Framework;
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
using Monocle;
|
||||
|
||||
|
@ -20,7 +20,7 @@ namespace Celeste.Mod.AvaliSkin {
|
|||
public enum Void {}
|
||||
|
||||
|
||||
public class ColorUtil {
|
||||
public static class ColorUtil {
|
||||
public static Color SettingToColor(CC col) {
|
||||
switch (col) {
|
||||
case CC.Beige: return new Color(162, 133, 92);
|
||||
|
@ -45,12 +45,31 @@ namespace Celeste.Mod.AvaliSkin {
|
|||
}
|
||||
}
|
||||
|
||||
public static string ColorToHex(Color color) {
|
||||
public static string ToHex(this Color color) {
|
||||
return $"#{color.R:x2}{color.G:x2}{color.B:x2}";
|
||||
}
|
||||
|
||||
public static Color HexToColor(string hex) {
|
||||
return Monocle.Calc.HexToColor(hex);
|
||||
public static string ToHexA(this Color color) {
|
||||
return $"#{color.R:x2}{color.G:x2}{color.B:x2}{color.A:x2}";
|
||||
}
|
||||
|
||||
public static Color HexToColor(this string hex) {
|
||||
hex = hex.TrimStart('#');
|
||||
if (hex.Length < 6) {
|
||||
throw new IndexOutOfRangeException("Hex colors must contain at least 6 characters.");
|
||||
}
|
||||
|
||||
float r = (float)(Calc.HexToByte(hex[0]) * 16 + Calc.HexToByte(hex[1])) / 255f;
|
||||
float g = (float)(Calc.HexToByte(hex[2]) * 16 + Calc.HexToByte(hex[3])) / 255f;
|
||||
float b = (float)(Calc.HexToByte(hex[4]) * 16 + Calc.HexToByte(hex[5])) / 255f;
|
||||
|
||||
if (hex.Length < 8) {
|
||||
return new Color(r, g, b);
|
||||
}
|
||||
|
||||
float a = (float)(Calc.HexToByte(hex[6]) * 16 + Calc.HexToByte(hex[7])) / 255f;
|
||||
// premultiply the alpha
|
||||
return new Color(r, g, b) * a;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue