public static EditableColorPaletteEntry Parse(JsonObject data, IColorPaletteEntry sourceColor, IReadOnlyList <ContrastColorWrapper> contrastColors) { Color customColor; if (data.ContainsKey("CustomColor")) { customColor = data["CustomColor"].GetColor(); } else { customColor = default(Color); } bool useCustomColor = false; if (data.ContainsKey("UseCustomColor")) { useCustomColor = data["UseCustomColor"].GetBoolean(); } ColorStringFormat activeColorStringFormat = ColorStringFormat.PoundRGB; if (data.ContainsKey("ActiveColorStringFormat")) { activeColorStringFormat = data.GetEnum <ColorStringFormat>(); } return(new EditableColorPaletteEntry(sourceColor, customColor, useCustomColor, data.GetOptionalString("Title"), data.GetOptionalString("Description"), activeColorStringFormat, contrastColors)); }
public ColorPaletteEntry(Color color, string title, string description, ColorStringFormat activeColorStringFormat, IReadOnlyList <ContrastColorWrapper> contrastColors) { _activeColor = color; _title = title; _description = description; _activeColorStringFormat = activeColorStringFormat; ContrastColors = contrastColors; }
public static string FormatColorString(Color input, ColorStringFormat format, int precision = 4) { switch (format) { case ColorStringFormat.RGB: return(string.Format("{0}{1}{2}", input.R.ToString("x2"), input.G.ToString("x2"), input.B.ToString("x2"))); case ColorStringFormat.PoundRGB: return(string.Format("#{0}{1}{2}", input.R.ToString("X2"), input.G.ToString("X2"), input.B.ToString("X2"))); case ColorStringFormat.ARGB: return(string.Format("{0}{1}{2}{3}", input.A.ToString("x2"), input.R.ToString("x2"), input.G.ToString("x2"), input.B.ToString("x2"))); case ColorStringFormat.NormalizedRGB: var normalizedRGB = new NormalizedRGB(input, true, precision); return(string.Format("{0},{1},{2}", normalizedRGB.R, normalizedRGB.G, normalizedRGB.B)); case ColorStringFormat.HSL: var hsl = RGBToHSL(input, true, precision); return(string.Format("{0},{1},{2}", hsl.H, hsl.S, hsl.L)); case ColorStringFormat.HSV: var hsv = RGBToHSV(input, true, precision); return(string.Format("{0},{1},{2}", hsv.H, hsv.S, hsv.V)); case ColorStringFormat.LAB: var lab = RGBToLAB(input, true, precision); return(string.Format("{0},{1},{2}", lab.L, lab.A, lab.B)); case ColorStringFormat.LCH: var lch = RGBToLCH(input, true, precision); return(string.Format("{0},{1},{2}", lch.L, lch.C, lch.H)); case ColorStringFormat.XYZ: var xyz = RGBToXYZ(input, true, precision); return(string.Format("{0},{1},{2}", xyz.X, xyz.Y, xyz.Z)); case ColorStringFormat.Luminance: var lum = RGBToLuminance(input, true, precision); return(lum.ToString()); case ColorStringFormat.Temperature: var temp = RGBToTemperature(input, true, 0); return(temp.ToString()); } return(null); }
public EditableColorPaletteEntry(IColorPaletteEntry sourceColor, Color customColor, bool useCustomColor, string title, string description, ColorStringFormat activeColorStringFormat, IReadOnlyList <ContrastColorWrapper> contrastColors) { _sourceColor = sourceColor; _customColor = customColor; _useCustomColor = useCustomColor; _title = title; _description = description; _activeColorStringFormat = activeColorStringFormat; if (_useCustomColor || _sourceColor == null) { _activeColor = _customColor; } else { _activeColor = _sourceColor.ActiveColor; } ContrastColors = contrastColors; }
public static ColorPaletteEntry Parse(JsonObject data, IReadOnlyList <ContrastColorWrapper> contrastColors) { Color color; if (data.ContainsKey("Color")) { color = data["Color"].GetColor(); } else { color = default(Color); } ColorStringFormat activeColorStringFormat = ColorStringFormat.PoundRGB; if (data.ContainsKey("ActiveColorStringFormat")) { activeColorStringFormat = data.GetEnum <ColorStringFormat>(); } return(new ColorPaletteEntry(color, data.GetOptionalString("Title"), data.GetOptionalString("Description"), activeColorStringFormat, contrastColors)); }
/// <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)); }