/// <summary> /// /// </summary> /// <param name="value"></param> public HsbColorFNormalized(IHsbColorModel <byte> value) { if (value == null) { throw new ArgumentNullException(); } if (value.Hue < 0f || value.Hue > ColorExtensions.HUE_MAXVALUE) { throw new ArgumentOutOfRangeException("hue"); } if (value.Saturation < 0f || value.Saturation > 1f) { throw new ArgumentOutOfRangeException("saturation"); } if (value.Brightness < 0f || value.Brightness > 1f) { throw new ArgumentOutOfRangeException("brightness"); } if (value.Alpha < 0f || value.Alpha > 1f) { throw new ArgumentOutOfRangeException("alpha"); } ColorExtensions.HSBtoRGB((value.Hue == ColorExtensions.HUE_MAXVALUE) ? 0f : value.Hue, value.Saturation, value.Brightness, out float r, out float g, out float b); ColorExtensions.RGBtoHSB(r, g, b, out float hue, out float saturation, out float brightness); _hue = hue; _saturation = saturation; _brightness = brightness; _alpha = value.Alpha; }
/// <summary> /// Indicates whether the current value is equal to another color model object. /// </summary> /// <param name="other">A color model to compare with this value.</param> /// <param name="exact"><c>true</c> to compare exact values; otherwise, <c>false</c> to compare normalized values.</param> /// <returns><c>true</c> if the current value is equal to another color model object; otherwise, <c>false</c>.</returns> public bool Equals(IRgbColorModel <float> other, bool exact) { if (other == null) { return(false); } float b; if (exact) { if (other.Alpha != _alpha.ToPercentage()) { return(false); } ColorExtensions.RGBtoHSB(other.Red, other.Green, other.Blue, out float h, out float s, out b); return(_hue.ToDegrees() == h && _saturation.ToDegrees() == s && _brightness.ToDegrees() == b); } if (other.Alpha.FromPercentage() != _alpha) { return(false); } ColorExtensions.HSBtoRGB(_hue.ToDegrees(), _saturation.ToPercentage(), _brightness.ToPercentage(), out float r, out float g, out b); return(other.Red == r.FromPercentage() && other.Green == g.FromPercentage() && other.Blue == b.FromPercentage()); }
/// <summary> /// /// </summary> /// <param name="value"></param> public HsbColor32Normalized(IHsbColorModel <float> value) { if (value == null) { throw new ArgumentNullException(); } _value = 0; if (value.Hue < 0f || value.Hue > ColorExtensions.HUE_MAXVALUE) { throw new ArgumentOutOfRangeException("hue"); } if (value.Saturation < 0f || value.Saturation > 1f) { throw new ArgumentOutOfRangeException("saturation"); } if (value.Brightness < 0f || value.Brightness > 1f) { throw new ArgumentOutOfRangeException("brightness"); } if (value.Alpha < 0f || value.Alpha > 1f) { throw new ArgumentOutOfRangeException("alpha"); } ColorExtensions.HSBtoRGB(value.Hue, value.Saturation, value.Brightness, out float r, out float g, out float b); ColorExtensions.RGBtoHSB(r, g, b, out float hF, out float sF, out float bF); _hue = hF.FromDegrees(); _brightness = bF.FromPercentage(); _saturation = sF.FromPercentage(); _alpha = value.Alpha.FromPercentage();; }
/// <summary> /// /// </summary> /// <param name="hue"></param> /// <param name="saturation"></param> /// <param name="brightness"></param> /// <param name="alpha"></param> public HsbColorFNormalized(float hue, float saturation, float brightness, float alpha) { if (hue < 0f || hue > ColorExtensions.HUE_MAXVALUE) { throw new ArgumentOutOfRangeException("hue"); } if (saturation < 0f || saturation > 1f) { throw new ArgumentOutOfRangeException("saturation"); } if (brightness < 0f || brightness > 1f) { throw new ArgumentOutOfRangeException("brightness"); } if (alpha < 0f || alpha > 1f) { throw new ArgumentOutOfRangeException("alpha"); } ColorExtensions.HSBtoRGB((hue == ColorExtensions.HUE_MAXVALUE) ? 0f : hue, saturation, brightness, out float r, out float g, out float b); ColorExtensions.RGBtoHSB(r, g, b, out hue, out saturation, out brightness); _hue = hue; _saturation = saturation; _brightness = brightness; _alpha = alpha; }
/// <summary> /// /// </summary> /// <param name="other"></param> /// <returns></returns> public HsbColorFNormalized MergeAverage(IEnumerable <IHsbColorModel <float> > other) { if (other == null) { return(this); } ColorExtensions.HSBtoRGB(_hue, _saturation, _brightness, out float rF, out float gF, out float bF); double r = rF, g = gF, b = bF, a = _alpha; double brightness = _brightness; int count = 0; foreach (IHsbColorModel <float> item in other) { if (item != null) { count++; ColorExtensions.HSBtoRGB(item.Hue, item.Saturation, item.Brightness, out rF, out gF, out bF); r += (double)rF; g += (double)gF; b += (double)bF; a += (double)item.Alpha; brightness += (double)item.Brightness; } } if (count == 0) { return(this); } ColorExtensions.RGBtoHSB((float)(r / (double)count), (float)(r / (double)count), (float)(r / (double)count), out float h, out float s, out bF); return(new HsbColorFNormalized(h, s, bF, (float)(a / (double)count))); }
/// <summary> /// /// </summary> /// <param name="other"></param> /// <returns></returns> public HsbColor32Normalized MergeAverage(IEnumerable <IHsbColorModel <byte> > other) { if (other == null) { return(this); } ColorExtensions.HSBtoRGB(_hue.ToDegrees(), _saturation.ToPercentage(), _brightness.ToPercentage(), out float rF, out float gF, out float bF); double r = rF, g = gF, b = bF, a = _alpha.ToPercentage(); double brightness = _brightness.ToPercentage(); int count = 0; foreach (IHsbColorModel <byte> item in other) { if (item != null) { count++; ColorExtensions.HSBtoRGB(item.Hue.ToPercentage(), item.Saturation.ToPercentage(), item.Brightness.ToPercentage(), out rF, out gF, out bF); r += (double)rF; g += (double)gF; b += (double)bF; a += (double)item.Alpha.ToPercentage(); brightness += (double)item.Brightness.ToPercentage(); } } if (count == 0) { return(this); } ColorExtensions.RGBtoHSB((float)(r / (double)count), (float)(r / (double)count), (float)(r / (double)count), out float h, out float s, out bF); return(new HsbColor32Normalized(h.FromDegrees(), s.FromPercentage(), bF.FromPercentage(), ((float)(a / (double)count)).FromPercentage())); }
/// <summary> /// /// </summary> /// <param name="other"></param> /// <param name="exact"></param> /// <returns></returns> public bool Equals(IHsbColorModel <byte> other, bool exact) { if (other == null) { return(false); } float b; if (exact) { if (other.Alpha.ToPercentage() != _alpha) { return(false); } ColorExtensions.HSBtoRGB(other.Hue.ToDegrees(), other.Saturation.ToPercentage(), other.Brightness.ToPercentage(), out float r, out float g, out b); return(_red == r && _green == g && _blue == b); } if (other.Alpha != _alpha.FromPercentage()) { return(false); } if (!other.IsNormalized) { other = other.AsNormalized(); } ColorExtensions.RGBtoHSB(_red, _green, _blue, out float h, out float s, out b); return(other.Hue == h.FromDegrees() && other.Saturation == s.FromPercentage() && other.Brightness == b.FromPercentage()); }
/// <summary> /// Indicates whether the current value is equal to a <seealso cref="System.Windows.Media.Color"/> object. /// </summary> /// <param name="other">A <seealso cref="System.Windows.Media.Color"/> object to compare with this value.</param> /// <returns><c>true</c> if the current value is equal to the specified <seealso cref="System.Windows.Media.Color"/> object; otherwise, <c>false</c>.</returns> public bool Equals(System.Windows.Media.Color other) { if (other.A != _alpha) { return(false); } ColorExtensions.HSBtoRGB(_hue.ToDegrees(), _saturation.ToPercentage(), _brightness.ToPercentage(), out float r, out float g, out float b); return(r.FromPercentage() == other.R && g.FromPercentage() == other.G && b.FromPercentage() == other.B); }
/// <summary> /// /// </summary> /// <param name="other"></param> /// <returns></returns> public bool Equals(System.Drawing.Color other) { if (other.A != _alpha.FromPercentage()) { return(false); } ColorExtensions.HSBtoRGB(_hue, _saturation, _brightness, out float r, out float g, out float b); return(r.FromPercentage() == other.R && g.FromPercentage() == other.G && b.FromPercentage() == other.B); }
/// <summary> /// /// </summary> /// <param name="value"></param> public HsbColorFNormalized(HsbColorF value) { ColorExtensions.HSBtoRGB(value.Hue, value.Saturation, value.Brightness, out float r, out float g, out float b); ColorExtensions.RGBtoHSB(r, g, b, out float hue, out float saturation, out b); _hue = hue; _saturation = saturation; _brightness = b; _alpha = value.Alpha; }
/// <summary> /// /// </summary> /// <param name="hue"></param> /// <param name="saturation"></param> /// <param name="brightness"></param> /// <param name="alpha"></param> public HsbColor32Normalized(byte hue, byte saturation, byte brightness, byte alpha) { _value = 0; ColorExtensions.HSBtoRGB(hue.ToDegrees(), saturation.ToPercentage(), brightness.ToPercentage(), out float r, out float g, out float b); ColorExtensions.RGBtoHSB(r, g, b, out float hF, out float sF, out float bF); _hue = hF.FromDegrees(); _brightness = bF.FromPercentage(); _saturation = sF.FromPercentage(); _alpha = alpha; }
/// <summary> /// /// </summary> /// <param name="value"></param> public HsbColor32Normalized(HsbColor32 value) { _value = 0; ColorExtensions.HSBtoRGB(value.Hue.ToDegrees(), value.Saturation.ToPercentage(), value.Brightness.ToPercentage(), out float r, out float g, out float b); ColorExtensions.RGBtoHSB(r, g, b, out float hF, out float sF, out float bF); _hue = hF.FromDegrees(); _brightness = bF.FromPercentage(); _saturation = sF.FromPercentage(); _alpha = value.Alpha; }
/// <summary> /// /// </summary> /// <param name="ahsb"></param> public HsbColor32Normalized(int ahsb) { byte[] values = BitConverter.GetBytes(ahsb); ColorExtensions.HSBtoRGB(values[2].ToDegrees(), values[1].ToPercentage(), values[0].ToPercentage(), out float r, out float g, out float b); ColorExtensions.RGBtoHSB(r, g, b, out float hue, out float saturation, out float brightness); _hue = hue.FromDegrees(); _saturation = saturation.FromPercentage(); _brightness = brightness.FromPercentage(); _alpha = values[3]; }
/// <summary> /// /// </summary> /// <param name="value"></param> public RgbColorF(IHsbColorModel <byte> value) { if (value == null) { throw new ArgumentNullException(); } ColorExtensions.HSBtoRGB(value.Hue.ToDegrees(), value.Saturation.ToPercentage(), value.Brightness.ToPercentage(), out float r, out float g, out float b); _red = r; _green = g; _blue = b; _alpha = value.Alpha.ToPercentage(); }
/// <summary> /// Indicates whether the current value is equal to another color model object. /// </summary> /// <param name="other">A color model to compare with this value.</param> /// <param name="exact"><c>true</c> to compare exact values; otherwise, <c>false</c> to compare normalized values.</param> /// <returns><c>true</c> if the current value is equal to another color model object; otherwise, <c>false</c>.</returns> public bool Equals(IRgbColorModel <byte> other, bool exact) { if (other == null || _alpha != other.Alpha) { return(false); } float b; if (exact) { ColorExtensions.RGBtoHSB(other.Red, other.Green, other.Blue, out float h, out float s, out b); return(_hue == h.FromDegrees() && _saturation == s.FromPercentage() && _brightness == b.FromPercentage()); } ColorExtensions.HSBtoRGB(_hue, _saturation, _brightness, out float r, out float g, out b); return(other.Red == r.FromPercentage() && other.Green == g.FromPercentage() && other.Blue == b.FromPercentage()); }
/// <summary> /// /// </summary> /// <param name="other"></param> /// <param name="exact"></param> /// <returns></returns> public bool Equals(IRgbColorModel <float> other, bool exact) { if (other == null || _alpha != other.Alpha) { return(false); } float b; if (exact) { ColorExtensions.RGBtoHSB(other.Red, other.Green, other.Blue, out float h, out float s, out b); return(_hue == h && _saturation == s && _brightness == b); } ColorExtensions.HSBtoRGB(_hue, _saturation, _brightness, out float r, out float g, out b); return(other.Red == r && other.Green == g && other.Blue == b); }
/// <summary> /// Returns a <see cref="RgbColor32" /> value with the color saturation adjusted. /// </summary> /// <param name="percentage">The percentage to saturate the color, ranging from -1.0 to 1.0. A positive value increases saturation, a negative value decreases saturation and a zero vale has no effect.</param> /// <returns>A <see cref="RgbColor32" /> value with the color saturation adjusted.</returns> /// <remarks>For positive values, the target saturation value is determined using the following formula: <c>saturation + (MAX_VALUE - saturation) * percentage</c> /// <para>For negative values, the target saturation value is determined using the following formula: <c>saturation + saturation * percentage</c></para></remarks> /// <exception cref="ArgumentOutOfRangeException"><paramref name="percentage" /> is less than -1.0 or <paramref name="percentage" /> is greater than 1.0.</exception> public RgbColor32 ShiftSaturation(float percentage) { if (percentage < -1f || percentage > 1f) { throw new ArgumentOutOfRangeException("percentage"); } if (percentage == 0f) { return(this); } ColorExtensions.RGBtoHSB(_red.ToPercentage(), _green.ToPercentage(), _blue.ToPercentage(), out float h, out float s, out float b); if ((percentage == 1f) ? s == 1f : percentage == -1f && s == 0f) { return(this); } ColorExtensions.HSBtoRGB(h, s + ((percentage > 0f) ? (1f - s) : s) * percentage, b, out float r, out float g, out b); return(new RgbColor32(r.FromPercentage(), g.FromPercentage(), b.FromPercentage(), _alpha)); }
/// <summary> /// Returns a <see cref="RgbColorF" /> value with the color brightness adjusted. /// </summary> /// <param name="percentage">The percentage to saturate the color, ranging from -1.0 to 1.0. A positive value increases brightness, a negative value decreases brightness and a zero vale has no effect.</param> /// <returns>A <see cref="RgbColorF" /> value with the color brightness adjusted.</returns> /// <remarks>For positive values, the target brightness value is determined using the following formula: <c>brightness + (1.0 - brightness) * percentage</c> /// <para>For negative values, the target brightness value is determined using the following formula: <c>brightness + brightness * percentage</c></para></remarks> /// <exception cref="ArgumentOutOfRangeException"><paramref name="percentage" /> is less than -1.0 or <paramref name="percentage" /> is greater than 1.0.</exception> public RgbColorF ShiftBrightness(float percentage) { if (percentage < -1f || percentage > 1f) { throw new ArgumentOutOfRangeException("percentage"); } if (percentage == 0f) { return(this); } ColorExtensions.RGBtoHSB(_red, _green, _blue, out float h, out float s, out float b); if ((percentage == 1f) ? b == 1f : percentage == -1f && b == 0f) { return(this); } ColorExtensions.HSBtoRGB(h, s, b + ((percentage > 0f) ? (1f - b) : b) * percentage, out float r, out float g, out b); return(new RgbColorF(r, g, b, _alpha)); }
/// <summary> /// /// </summary> /// <param name="value"></param> public RgbColorF(IHsbColorModel <float> value) { if (value == null) { throw new ArgumentNullException(); } if (value.Alpha < 0f || value.Alpha > 1f) { throw new ArgumentOutOfRangeException("value", "Value for alpha is out of range"); } try { ColorExtensions.HSBtoRGB(value.Hue, value.Saturation, value.Brightness, out float r, out float g, out float b); _red = r; _green = g; _blue = b; } catch (ArgumentOutOfRangeException exc) { throw new ArgumentOutOfRangeException("value", "Value for " + exc.ParamName + " is out of range"); } _alpha = value.Alpha; }
/// <summary> /// Returns a <see cref="RgbColorF" /> value with the color hue adjusted. /// </summary> /// <param name="degrees">The number of degrees to shift the hue value, ranging from -360.0 to 360.0. A positive value shifts the hue in the red-to-cyan direction, and a negative value shifts the hue in the cyan-to-red direction.</param> /// <returns>A <see cref="RgbColorF" /> value with the color hue adjusted.</returns> /// <remarks>The values 0.0, -360.0 and 360.0 have no effect since they would result in no hue change.</remarks> /// <exception cref="ArgumentOutOfRangeException"><paramref name="degrees" /> is less than -360.0 or <paramref name="degrees" /> is greater than 360.0.</exception> public RgbColorF ShiftHue(float degrees) { if (degrees < -360f || degrees > 360f) { throw new ArgumentOutOfRangeException("degrees"); } if (degrees == 0f || degrees == 360f || degrees == -360f) { return(this); } ColorExtensions.RGBtoHSB(_red, _green, _blue, out float h, out float s, out float b); h += degrees; if (h < 0f) { h += 360f; } else if (h >= 360f) { h -= 360f; } ColorExtensions.HSBtoRGB(h, s, b, out float r, out float g, out b); return(new RgbColorF(r, g, b, _alpha)); }
/// <summary> /// /// </summary> /// <param name="other"></param> /// <param name="exact"></param> /// <returns></returns> public bool Equals(IHsbColorModel <float> other, bool exact) { if (other == null || _alpha != other.Alpha) { return(false); } float b; if (exact) { ColorExtensions.RGBtoHSB(_red, _green, _blue, out float h, out float s, out b); return(other.Hue == h && other.Saturation == s && other.Brightness == b); } if (!other.IsNormalized) { other = other.AsNormalized(); } ColorExtensions.HSBtoRGB(other.Hue, other.Saturation, other.Brightness, out float r, out float g, out b); return(_red == r && _green == g && _blue == b); }
/// <summary> /// /// </summary> /// <param name="other"></param> /// <returns></returns> public RgbColorF MergeAverage(IEnumerable <IRgbColorModel <float> > other) { if (other == null) { return(this); } double r = _red, g = _green, b = _blue, a = _alpha; ColorExtensions.RGBtoHSB(_red, _green, _blue, out float h, out float s, out float bF); double brightness = bF; int count = 0; foreach (IRgbColorModel <float> item in other) { if (item != null) { count++; r += (double)item.Red; g += (double)item.Green; b += (double)item.Blue; a += (double)item.Alpha; ColorExtensions.RGBtoHSB(item.Red, item.Green, item.Blue, out h, out s, out bF); brightness += (double)bF; } } if (count == 0) { return(this); } ColorExtensions.RGBtoHSB((float)(r / (double)count), (float)(r / (double)count), (float)(r / (double)count), out h, out s, out bF); ColorExtensions.HSBtoRGB(h, s, (float)(brightness / (double)count), out float red, out float green, out bF); return(new RgbColorF(red, green, bF, (float)(a / (double)count))); }
/// <summary> /// /// </summary> /// <param name="other"></param> /// <returns></returns> public RgbColor32 MergeAverage(IEnumerable <IRgbColorModel <byte> > other) { if (other == null) { return(this); } double r = _red.ToPercentage(), g = _green.ToPercentage(), b = _blue.ToPercentage(), a = _alpha.ToPercentage(); ColorExtensions.RGBtoHSB(_red.ToPercentage(), _green.ToPercentage(), _blue.ToPercentage(), out float h, out float s, out float bF); double brightness = bF; int count = 0; foreach (IRgbColorModel <byte> item in other) { if (item != null) { count++; r += (double)item.Red.ToPercentage(); g += (double)item.Green.ToPercentage(); b += (double)item.Blue.ToPercentage(); a += (double)item.Alpha.ToPercentage(); ColorExtensions.RGBtoHSB(item.Red.ToPercentage(), item.Green.ToPercentage(), item.Blue.ToPercentage(), out h, out s, out bF); brightness += (double)bF; } } if (count == 0) { return(this); } ColorExtensions.RGBtoHSB((float)(r / (double)count), (float)(r / (double)count), (float)(r / (double)count), out h, out s, out bF); ColorExtensions.HSBtoRGB(h, s, (float)(brightness / (double)count), out float red, out float green, out bF); return(new RgbColor32(red.FromPercentage(), green.FromPercentage(), bF.FromPercentage(), ((float)(a / (double)count)).FromPercentage())); }
/// <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")); }