/// <summary> /// /// </summary> /// <param name="value"></param> public RgbColor32(RgbColorF value) { _red = value.Red.FromPercentage(); _green = value.Green.FromPercentage(); _blue = value.Blue.FromPercentage(); _alpha = value.Alpha.FromPercentage(); }
/// <summary> /// /// </summary> /// <param name="value"></param> public HsbColor32Normalized(RgbColorF value) { ColorExtensions.RGBtoHSB(value.Red, value.Green, value.Blue, out float h, out float s, out float b); _hue = h.FromDegrees(); _saturation = s.FromPercentage(); _brightness = b.FromPercentage(); _alpha = value.Alpha.FromPercentage(); }
/// <summary> /// /// </summary> /// <param name="value"></param> public HsbColorFNormalized(RgbColorF value) { ColorExtensions.RGBtoHSB(value.Red, value.Green, value.Blue, out float h, out float s, out float b); _hue = h; _saturation = s; _brightness = b; _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.ToPercentage(), _green.ToPercentage(), _blue.ToPercentage(), out h, out s, out b); return(HsbColor32.ToHexidecimalString(h.FromDegrees(), s.FromPercentage(), b.FromPercentage(), _alpha, false)); case ColorStringFormat.HSLAHexOpt: ColorExtensions.RGBtoHSB(_red.ToPercentage(), _green.ToPercentage(), _blue.ToPercentage(), out h, out s, out b); return(HsbColor32.ToHexidecimalString(h.FromDegrees(), s.FromPercentage(), b.FromPercentage(), _alpha, true)); case ColorStringFormat.HSLAPercent: ColorExtensions.RGBtoHSB(_red.ToPercentage(), _green.ToPercentage(), _blue.ToPercentage(), out h, out s, out b); return(HsbColorF.ToPercentParameterString(h, s, b, _alpha.ToPercentage())); case ColorStringFormat.HSLAValues: ColorExtensions.RGBtoHSB(_red.ToPercentage(), _green.ToPercentage(), _blue.ToPercentage(), out h, out s, out b); return(HsbColor32.ToValueParameterString(h.FromDegrees(), s.FromPercentage(), b.FromPercentage(), _alpha)); case ColorStringFormat.HSLHex: ColorExtensions.RGBtoHSB(_red.ToPercentage(), _green.ToPercentage(), _blue.ToPercentage(), out h, out s, out b); return(HsbColor32.ToHexidecimalString(h.FromDegrees(), s.FromPercentage(), b.FromPercentage(), false)); case ColorStringFormat.HSLHexOpt: ColorExtensions.RGBtoHSB(_red.ToPercentage(), _green.ToPercentage(), _blue.ToPercentage(), out h, out s, out b); return(HsbColor32.ToHexidecimalString(h.FromDegrees(), s.FromPercentage(), b.FromPercentage(), true)); case ColorStringFormat.HSLPercent: ColorExtensions.RGBtoHSB(_red.ToPercentage(), _green.ToPercentage(), _blue.ToPercentage(), out h, out s, out b); return(HsbColorF.ToPercentParameterString(h, s, b)); case ColorStringFormat.HSLValues: ColorExtensions.RGBtoHSB(_red.ToPercentage(), _green.ToPercentage(), _blue.ToPercentage(), out h, out s, out b); return(HsbColor32.ToValueParameterString(h.FromDegrees(), s.FromPercentage(), b.FromPercentage())); case ColorStringFormat.RGBAHexOpt: return(ToHexidecimalString(_red, _green, _blue, _alpha, true)); case ColorStringFormat.RGBAPercent: return(RgbColorF.ToPercentParameterString(_red.ToPercentage(), _green.ToPercentage(), _blue.ToPercentage(), _alpha.ToPercentage())); case ColorStringFormat.RGBAValues: return(ToValueParameterString(_red, _green, _blue, _alpha)); case ColorStringFormat.RGBHex: return(ToHexidecimalString(_red, _green, _blue, false)); case ColorStringFormat.RGBHexOpt: return(ToHexidecimalString(_red, _green, _blue, true)); case ColorStringFormat.RGBPercent: return(RgbColorF.ToPercentParameterString(_red.ToPercentage(), _green.ToPercentage(), _blue.ToPercentage())); case ColorStringFormat.RGBValues: return(ToValueParameterString(_red, _green, _blue)); } return(_value.ToString("X8")); }
internal static bool TryParse(string text, bool strict, out HsbColor32 result) { if (text != null && (text = text.Trim()).Length > 0) { Match match = ParseRegex.Match(text); if (match.Success) { try { if (match.Groups["h"].Success) { switch (text.Length) { case 3: result = new HsbColor32(int.Parse(new string(new char[] { text[0], text[0], text[1], text[1], text[2], text[2] }), NumberStyles.HexNumber) << 8); break; case 4: result = new HsbColor32(int.Parse(new string(new char[] { text[0], text[0], text[1], text[1], text[2], text[2] }), NumberStyles.HexNumber) << 8 | int.Parse(new string(new char[] { text[3], text[3] }))); break; case 8: result = new HsbColor32(int.Parse(text.Substring(0, 6), NumberStyles.HexNumber) << 8 | int.Parse(text.Substring(6), NumberStyles.HexNumber)); break; default: result = new HsbColor32(int.Parse(text, NumberStyles.HexNumber) << 8); break; } return(true); } float alpha = 100f; if (!match.Groups["a"].Success || (((match.Groups["a"].Value.EndsWith("%")) ? (float.TryParse(match.Groups["a"].Value.Substring(0, match.Groups["a"].Length - 1), out alpha) && (alpha = alpha / 100f) <= 1f) : (float.TryParse(match.Groups["a"].Value, out alpha) && alpha <= 1f)) && alpha >= 0f)) { if (match.Groups["b"].Success) { int h, s, l; if (int.TryParse(match.Groups["h"].Value, out h) && h > -1 && h <256 && int.TryParse(match.Groups["s"].Value, out s) && s> -1 && s <256 && int.TryParse(match.Groups["l"].Value, out l) && l> -1 && l < 256) { result = new HsbColor32((byte)h, (byte)s, (byte)l, alpha.FromPercentage()); return(true); } } else if (float.TryParse(match.Groups["h"].Value, out float hF) && hF >= 0f && hF <= 360f && float.TryParse(match.Groups["s"].Value, out float sF) && sF >= 0f && sF <= 100f && float.TryParse(match.Groups["l"].Value, out float lF) && lF >= 0f && lF <= 100f) { result = new HsbColor32(hF.FromDegrees(), (sF / 100f).FromPercentage(), (lF / 100f).FromPercentage(), alpha.FromPercentage()); return(true); } } } catch { } } else if (!strict && RgbColorF.TryParse(text, true, out RgbColorF rgb)) { ColorExtensions.RGBtoHSB(rgb.Red, rgb.Blue, rgb.Green, out float h, out float s, out float b); result = new HsbColor32(h.FromDegrees(), s.FromPercentage(), b.FromPercentage(), rgb.Alpha.FromPercentage()); return(true); } } result = default(HsbColor32); return(false); }