/// <summary> /// /// </summary> /// <param name="value"></param> public RgbColorF(RgbColor32 value) { _red = value.Red.ToPercentage(); _green = value.Green.ToPercentage(); _blue = value.Blue.ToPercentage(); _alpha = value.Alpha.ToPercentage(); }
/// <summary> /// /// </summary> /// <param name="value"></param> public HsbColor32Normalized(RgbColor32 value) { ColorExtensions.RGBtoHSB(value.Red.ToPercentage(), value.Green.ToPercentage(), value.Blue.ToPercentage(), out float h, out float s, out float b); _hue = h.FromDegrees(); _saturation = s.FromPercentage(); _brightness = b.FromPercentage(); _alpha = value.Alpha; }
/// <summary> /// /// </summary> /// <param name="obj"></param> /// <param name="result"></param> /// <returns></returns> public static bool TryGetColor(PSObject obj, out IColorModel result) { if (obj != null) { object baseObj = obj.BaseObject; if (baseObj is IColorModel) { result = (IColorModel)baseObj; return(true); } string[] names = new string[] { "Hue", "Saturation", "Brightness" }; StringComparer comparer = StringComparer.InvariantCultureIgnoreCase; object alpha = obj.Properties.Where(p => comparer.Equals(p.Name, "Alpha") && p.IsInstance && p.IsGettable) .Select(p => AsSimplestType(p.Value)).Where(o => o != null && (o is float || o is byte)).ToArray(); object[] hsb = names.Select(n => obj.Properties.FirstOrDefault(p => comparer.Equals(p.Name, n))).Where(p => p != null && p.IsInstance && p.IsGettable) .Select(p => AsSimplestType(p.Value)).Where(o => o != null).ToArray(); if (hsb.Length != 3) { names = new string[] { "Hue", "Saturation", "Lightness" }; hsb = names.Select(n => obj.Properties.FirstOrDefault(p => comparer.Equals(p.Name, n))).Where(p => p != null && p.IsInstance && p.IsGettable) .Select(p => AsSimplestType(p.Value)).Where(o => o != null).ToArray(); if (hsb.Length != 3) { names = new string[] { "H", "S", "L" }; hsb = names.Select(n => obj.Properties.FirstOrDefault(p => comparer.Equals(p.Name, n))).Where(p => p != null && p.IsInstance && p.IsGettable) .Select(p => AsSimplestType(p.Value)).Where(o => o != null).ToArray(); if (hsb.Length != 3) { names = new string[] { "H", "S", "B" }; hsb = names.Select(n => obj.Properties.FirstOrDefault(p => comparer.Equals(p.Name, n))).Where(p => p != null && p.IsInstance && p.IsGettable) .Select(p => AsSimplestType(p.Value)).Where(o => o != null).ToArray(); } } } if (hsb.Length == 3) { if (hsb[0] is byte) { if (hsb[1] is byte && hsb[2] is byte) { if (alpha == null) { result = new HsbColor32((byte)hsb[0], (byte)hsb[1], (byte)hsb[2]); return(true); } if (alpha is byte) { result = new HsbColor32((byte)hsb[0], (byte)hsb[1], (byte)hsb[2], (byte)alpha); return(true); } } } else if (hsb[0] is float h && h >= 0f && h <= 360f && hsb[1] is byte s && s >= 0f && s <= 1f && hsb[2] is byte b && b >= 0f && b <= 1f) { if (alpha == null) { result = new HsbColorF(h, s, b); return(true); } if (alpha is float && (float)alpha >= 0f && (float)alpha <= 1f) { result = new HsbColorF(h, s, b, (float)alpha); return(true); } } } names = new string[] { "Red", "Green", "Blue" }; hsb = names.Select(n => obj.Properties.FirstOrDefault(p => comparer.Equals(p.Name, n))).Where(p => p != null && p.IsInstance && p.IsGettable) .Select(p => AsSimplestType(p.Value)).Where(o => o != null).ToArray(); if (hsb.Length != 3) { names = new string[] { "R", "G", "B" }; hsb = names.Select(n => obj.Properties.FirstOrDefault(p => comparer.Equals(p.Name, n))).Where(p => p != null && p.IsInstance && p.IsGettable) .Select(p => AsSimplestType(p.Value)).Where(o => o != null).ToArray(); } if (hsb.Length == 3) { if (hsb[0] is byte) { if (hsb[1] is byte && hsb[2] is byte) { if (alpha == null) { result = new RgbColor32((byte)hsb[0], (byte)hsb[1], (byte)hsb[2]); return(true); } if (alpha is byte) { result = new RgbColor32((byte)hsb[0], (byte)hsb[1], (byte)hsb[2], (byte)alpha); return(true); } } } else if (hsb[0] is float h && h >= 0f && h <= 360f && hsb[1] is byte s && s >= 0f && s <= 1f && hsb[2] is byte b && b >= 0f && b <= 1f) { if (alpha == null) { result = new RgbColorF(h, s, b); return(true); } if (alpha is float && (float)alpha >= 0f && (float)alpha <= 1f) { result = new RgbColorF(h, s, b, (float)alpha); return(true); } } } } result = null; return(false); }
/// <summary> /// Gets formatted string representing the current color value. /// </summary> /// <param name="format">The color string format to use.</param> /// <returns>The formatted string representing the current color value.</returns> public string ToString(ColorStringFormat format) { float r, g, b; switch (format) { case ColorStringFormat.HSLAHexOpt: return(HsbColor32.ToHexidecimalString(_hue, _saturation, _brightness, _alpha, true)); case ColorStringFormat.HSLAPercent: return(HsbColorF.ToPercentParameterString(_hue.ToDegrees(), _saturation.ToPercentage(), _brightness.ToPercentage(), _alpha.ToPercentage())); case ColorStringFormat.HSLAValues: return(HsbColor32.ToValueParameterString(_hue, _saturation, _brightness, _alpha)); case ColorStringFormat.HSLHex: return(HsbColor32.ToHexidecimalString(_hue, _saturation, _brightness, false)); case ColorStringFormat.HSLHexOpt: return(HsbColor32.ToHexidecimalString(_hue, _saturation, _brightness, true)); case ColorStringFormat.HSLPercent: return(HsbColorF.ToPercentParameterString(_hue.ToDegrees(), _saturation.ToPercentage(), _brightness.ToPercentage())); case ColorStringFormat.HSLValues: return(HsbColor32.ToValueParameterString(_hue, _saturation, _brightness)); case ColorStringFormat.RGBAHex: ColorExtensions.HSBtoRGB(_hue.ToDegrees(), _saturation.ToPercentage(), _brightness.ToPercentage(), out r, out g, out b); return(RgbColor32.ToHexidecimalString(r.FromPercentage(), g.FromPercentage(), b.FromPercentage(), _alpha, false)); case ColorStringFormat.RGBAHexOpt: ColorExtensions.HSBtoRGB(_hue.ToDegrees(), _saturation.ToPercentage(), _brightness.ToPercentage(), out r, out g, out b); return(RgbColor32.ToHexidecimalString(r.FromPercentage(), g.FromPercentage(), b.FromPercentage(), _alpha, true)); case ColorStringFormat.RGBAPercent: ColorExtensions.HSBtoRGB(_hue.ToDegrees(), _saturation.ToPercentage(), _brightness.ToPercentage(), out r, out g, out b); return(RgbColorF.ToPercentParameterString(r, g, b, _alpha.ToPercentage())); case ColorStringFormat.RGBAValues: ColorExtensions.HSBtoRGB(_hue.ToDegrees(), _saturation.ToPercentage(), _brightness.ToPercentage(), out r, out g, out b); return(RgbColor32.ToValueParameterString(r.FromPercentage(), g.FromPercentage(), b.FromPercentage(), _alpha)); case ColorStringFormat.RGBHex: ColorExtensions.HSBtoRGB(_hue.ToDegrees(), _saturation.ToPercentage(), _brightness.ToPercentage(), out r, out g, out b); return(RgbColor32.ToHexidecimalString(r.FromPercentage(), g.FromPercentage(), b.FromPercentage(), false)); case ColorStringFormat.RGBHexOpt: ColorExtensions.HSBtoRGB(_hue.ToDegrees(), _saturation.ToPercentage(), _brightness.ToPercentage(), out r, out g, out b); return(RgbColor32.ToHexidecimalString(r.FromPercentage(), g.FromPercentage(), b.FromPercentage(), true)); case ColorStringFormat.RGBPercent: ColorExtensions.HSBtoRGB(_hue.ToDegrees(), _saturation.ToPercentage(), _brightness.ToPercentage(), out r, out g, out b); return(RgbColorF.ToPercentParameterString(r, g, b)); case ColorStringFormat.RGBValues: ColorExtensions.HSBtoRGB(_hue.ToDegrees(), _saturation.ToPercentage(), _brightness.ToPercentage(), out r, out g, out b); return(RgbColor32.ToValueParameterString(r.FromPercentage(), g.FromPercentage(), b.FromPercentage())); } return(_value.ToString("X8")); }
/// <summary> /// Gets formatted string representing the current color value. /// </summary> /// <param name="format">The color string format to use.</param> /// <returns>The formatted string representing the current color value.</returns> public string ToString(ColorStringFormat format) { float h, s, b; switch (format) { case ColorStringFormat.HSLAHex: ColorExtensions.RGBtoHSB(_red, _green, _blue, out h, out s, out b); return(HsbColor32.ToHexidecimalString(h.FromDegrees(), s.FromPercentage(), b.FromPercentage(), _alpha.FromPercentage(), false)); case ColorStringFormat.HSLAHexOpt: ColorExtensions.RGBtoHSB(_red, _green, _blue, out h, out s, out b); return(HsbColor32.ToHexidecimalString(h.FromDegrees(), s.FromPercentage(), b.FromPercentage(), _alpha.FromPercentage(), true)); case ColorStringFormat.HSLAPercent: ColorExtensions.RGBtoHSB(_red, _green, _blue, out h, out s, out b); return(HsbColorF.ToPercentParameterString(h, s, b, _alpha)); case ColorStringFormat.HSLAValues: ColorExtensions.RGBtoHSB(_red, _green, _blue, out h, out s, out b); return(HsbColor32.ToValueParameterString(h.FromDegrees(), s.FromPercentage(), b.FromPercentage(), _alpha)); case ColorStringFormat.HSLHex: ColorExtensions.RGBtoHSB(_red, _green, _blue, out h, out s, out b); return(HsbColor32.ToHexidecimalString(h.FromDegrees(), s.FromPercentage(), b.FromPercentage(), false)); case ColorStringFormat.HSLHexOpt: ColorExtensions.RGBtoHSB(_red, _green, _blue, out h, out s, out b); return(HsbColor32.ToHexidecimalString(h.FromDegrees(), s.FromPercentage(), b.FromPercentage(), true)); case ColorStringFormat.HSLPercent: ColorExtensions.RGBtoHSB(_red, _green, _blue, out h, out s, out b); return(HsbColorF.ToPercentParameterString(h, s, b)); case ColorStringFormat.HSLValues: ColorExtensions.RGBtoHSB(_red, _green, _blue, out h, out s, out b); return(HsbColor32.ToValueParameterString(h.FromDegrees(), s.FromPercentage(), b.FromPercentage())); case ColorStringFormat.RGBAHex: return(RgbColor32.ToHexidecimalString(_red.FromPercentage(), _green.FromPercentage(), _blue.FromPercentage(), _alpha.FromPercentage(), false)); case ColorStringFormat.RGBAHexOpt: return(RgbColor32.ToHexidecimalString(_red.FromPercentage(), _green.FromPercentage(), _blue.FromPercentage(), _alpha.FromPercentage(), true)); case ColorStringFormat.RGBAValues: return(RgbColor32.ToValueParameterString(_red.FromPercentage(), _green.FromPercentage(), _blue.FromPercentage(), _alpha.FromPercentage())); case ColorStringFormat.RGBHex: return(RgbColor32.ToHexidecimalString(_red.FromPercentage(), _green.FromPercentage(), _blue.FromPercentage(), false)); case ColorStringFormat.RGBHexOpt: return(RgbColor32.ToHexidecimalString(_red.FromPercentage(), _green.FromPercentage(), _blue.FromPercentage(), true)); case ColorStringFormat.RGBPercent: return(ToPercentParameterString(_red, _green, _blue)); case ColorStringFormat.RGBValues: return(RgbColor32.ToValueParameterString(_red.FromPercentage(), _green.FromPercentage(), _blue.FromPercentage())); } return(ToPercentParameterString(_red, _green, _blue, _alpha)); }