1
0
mirror of https://github.com/DarthAffe/RGB.NET.git synced 2025-12-12 17:48:31 +00:00

Refactored color

This commit is contained in:
Darth Affe 2019-02-24 17:53:04 +01:00
parent e70c1c6bb0
commit c1e2af6fbb
16 changed files with 929 additions and 1361 deletions

View File

@ -73,10 +73,8 @@ namespace RGB.NET.Brushes.Gradients
public Color GetColor(double offset)
{
double range = EndHue - StartHue;
double hue = (StartHue + (range * offset)) % 360f;
if (hue < 0)
hue += 360;
return Color.FromHSV(hue, 1, 1);
double hue = StartHue + (range * offset);
return HSVColor.Create(hue, 1, 1);
}
/// <inheritdoc />
@ -87,7 +85,7 @@ namespace RGB.NET.Brushes.Gradients
StartHue += offset;
EndHue += offset;
while ((StartHue > 360) && (EndHue > 360))
{
StartHue -= 360;

View File

@ -115,7 +115,7 @@ namespace RGB.NET.Core
// Since we use HSV to calculate there is no way to make a color 'brighter' than 100%
// Be carefull with the naming: Since we use HSV the correct term is 'value' but outside we call it 'brightness'
// THIS IS NOT A HSB CALCULATION!!!
return color.MultiplyValue(Brightness.Clamp(0, 1))
return color.MultiplyHSV(value: Brightness.Clamp(0, 1))
.MultiplyA(Opacity.Clamp(0, 1));
}

334
RGB.NET.Core/Color/Color.cs Normal file
View File

@ -0,0 +1,334 @@
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable UnusedMember.Global
// ReSharper disable UnusedMethodReturnValue.Global
using System;
using System.Diagnostics;
namespace RGB.NET.Core
{
/// <inheritdoc />
/// <summary>
/// Represents an ARGB (alpha, red, green, blue) color.
/// </summary>
[DebuggerDisplay("[A: {A}, R: {R}, G: {G}, B: {B}]")]
public struct Color
{
#region Constants
/// <summary>
/// Gets an transparent color [A: 0, R: 0, G: 0, B: 0]
/// </summary>
public static Color Transparent => new Color(0, 0, 0, 0);
#endregion
#region Properties & Fields
/// <summary>
/// Gets the alpha component value of this <see cref="Color"/> as byte in the range [0..255].
/// </summary>
public byte A { get; }
/// <summary>
/// Gets the alpha component value of this <see cref="Color"/> as percentage in the range [0..1].
/// </summary>
public double APercent { get; }
/// <summary>
/// Gets the red component value of this <see cref="Color"/> as byte in the range [0..255].
/// </summary>
public byte R { get; }
/// <summary>
/// Gets the red component value of this <see cref="Color"/> as percentage in the range [0..1].
/// </summary>
public double RPercent { get; }
/// <summary>
/// Gets the green component value of this <see cref="Color"/> as byte in the range [0..255].
/// </summary>
public byte G { get; }
/// <summary>
/// Gets the green component value of this <see cref="Color"/> as percentage in the range [0..1].
/// </summary>
public double GPercent { get; }
/// <summary>
/// Gets the blue component value of this <see cref="Color"/> as byte in the range [0..255].
/// </summary>
public byte B { get; }
/// <summary>
/// Gets the blue component value of this <see cref="Color"/> as percentage in the range [0..1].
/// </summary>
public double BPercent { get; }
#endregion
#region Constructors
/// <inheritdoc />
/// <summary>
/// Initializes a new instance of the <see cref="T:RGB.NET.Core.Color" /> struct using RGB-Values.
/// Alpha defaults to 255.
/// </summary>
/// <param name="r">The red component value of this <see cref="T:RGB.NET.Core.Color" />.</param>
/// <param name="g">The green component value of this <see cref="T:RGB.NET.Core.Color" />.</param>
/// <param name="b">The blue component value of this <see cref="T:RGB.NET.Core.Color" />.</param>
public Color(byte r, byte g, byte b)
: this(byte.MaxValue, r, g, b)
{ }
/// <inheritdoc />
/// <summary>
/// Initializes a new instance of the <see cref="T:RGB.NET.Core.Color" /> struct using RGB-Values.
/// Alpha defaults to 255.
/// </summary>
/// <param name="r">The red component value of this <see cref="T:RGB.NET.Core.Color" />.</param>
/// <param name="g">The green component value of this <see cref="T:RGB.NET.Core.Color" />.</param>
/// <param name="b">The blue component value of this <see cref="T:RGB.NET.Core.Color" />.</param>
public Color(int r, int g, int b)
: this((byte)r.Clamp(0, byte.MaxValue), (byte)g.Clamp(0, byte.MaxValue), (byte)b.Clamp(0, byte.MaxValue))
{ }
/// <summary>
/// Initializes a new instance of the <see cref="Color"/> struct using ARGB values.
/// </summary>
/// <param name="a">The alpha component value of this <see cref="Color"/>.</param>
/// <param name="r">The red component value of this <see cref="Color"/>.</param>
/// <param name="g">The green component value of this <see cref="Color"/>.</param>
/// <param name="b">The blue component value of this <see cref="Color"/>.</param>
public Color(byte a, byte r, byte g, byte b)
: this(a, r, g, b, a.GetPercentageFromByteValue(), r.GetPercentageFromByteValue(), g.GetPercentageFromByteValue(), b.GetPercentageFromByteValue())
{ }
/// <summary>
/// Initializes a new instance of the <see cref="Color"/> struct using ARGB values.
/// </summary>
/// <param name="a">The alpha component value of this <see cref="Color"/>.</param>
/// <param name="r">The red component value of this <see cref="Color"/>.</param>
/// <param name="g">The green component value of this <see cref="Color"/>.</param>
/// <param name="b">The blue component value of this <see cref="Color"/>.</param>
public Color(int a, int r, int g, int b)
: this((byte)a.Clamp(0, byte.MaxValue), (byte)r.Clamp(0, byte.MaxValue), (byte)g.Clamp(0, byte.MaxValue), (byte)b.Clamp(0, byte.MaxValue))
{ }
/// <summary>
/// Initializes a new instance of the <see cref="Color"/> struct using RGB-percent values.
/// Alpha defaults to 1.0.
/// </summary>
/// <param name="r">The red component value of this <see cref="Color"/>.</param>
/// <param name="g">The green component value of this <see cref="Color"/>.</param>
/// <param name="b">The blue component value of this <see cref="Color"/>.</param>
public Color(double r, double g, double b)
: this(1.0, r, g, b)
{ }
/// <summary>
/// Initializes a new instance of the <see cref="Color"/> struct using ARGB-percent values.
/// </summary>
/// <param name="a">The alpha component value of this <see cref="Color"/>.</param>
/// <param name="r">The red component value of this <see cref="Color"/>.</param>
/// <param name="g">The green component value of this <see cref="Color"/>.</param>
/// <param name="b">The blue component value of this <see cref="Color"/>.</param>
public Color(double a, byte r, byte g, byte b)
: this(a.GetByteValueFromPercentage(), r, g, b,
a.Clamp(0, 1), r.GetPercentageFromByteValue(), g.GetPercentageFromByteValue(), b.GetPercentageFromByteValue())
{ }
/// <summary>
/// Initializes a new instance of the <see cref="Color"/> struct using ARGB-percent values.
/// </summary>
/// <param name="a">The alpha component value of this <see cref="Color"/>.</param>
/// <param name="r">The red component value of this <see cref="Color"/>.</param>
/// <param name="g">The green component value of this <see cref="Color"/>.</param>
/// <param name="b">The blue component value of this <see cref="Color"/>.</param>
public Color(double a, int r, int g, int b)
: this(a, (byte)r.Clamp(0, byte.MaxValue), (byte)g.Clamp(0, byte.MaxValue), (byte)b.Clamp(0, byte.MaxValue))
{ }
/// <summary>
/// Initializes a new instance of the <see cref="Color"/> struct using ARGB-percent values.
/// </summary>
/// <param name="a">The alpha component value of this <see cref="Color"/>.</param>
/// <param name="r">The red component value of this <see cref="Color"/>.</param>
/// <param name="g">The green component value of this <see cref="Color"/>.</param>
/// <param name="b">The blue component value of this <see cref="Color"/>.</param>
public Color(int a, double r, double g, double b)
: this((byte)a.Clamp(0, byte.MaxValue), r, g, b)
{ }
/// <summary>
/// Initializes a new instance of the <see cref="Color"/> struct using ARGB-percent values.
/// </summary>
/// <param name="a">The alpha component value of this <see cref="Color"/>.</param>
/// <param name="r">The red component value of this <see cref="Color"/>.</param>
/// <param name="g">The green component value of this <see cref="Color"/>.</param>
/// <param name="b">The blue component value of this <see cref="Color"/>.</param>
public Color(byte a, double r, double g, double b)
: this(a, r.GetByteValueFromPercentage(), g.GetByteValueFromPercentage(), b.GetByteValueFromPercentage(),
a.GetPercentageFromByteValue(), r.Clamp(0, 1), g.Clamp(0, 1), b.Clamp(0, 1))
{ }
/// <summary>
/// Initializes a new instance of the <see cref="Color"/> struct using ARGB-percent values.
/// </summary>
/// <param name="a">The alpha component value of this <see cref="Color"/>.</param>
/// <param name="r">The red component value of this <see cref="Color"/>.</param>
/// <param name="g">The green component value of this <see cref="Color"/>.</param>
/// <param name="b">The blue component value of this <see cref="Color"/>.</param>
public Color(double a, double r, double g, double b)
: this(a.GetByteValueFromPercentage(), r.GetByteValueFromPercentage(), g.GetByteValueFromPercentage(), b.GetByteValueFromPercentage(),
a.Clamp(0, 1), r.Clamp(0, 1), g.Clamp(0, 1), b.Clamp(0, 1))
{ }
/// <inheritdoc />
/// <summary>
/// Initializes a new instance of the <see cref="T:RGB.NET.Core.Color" /> struct by cloning a existing <see cref="T:RGB.NET.Core.Color" />.
/// </summary>
/// <param name="color">The <see cref="T:RGB.NET.Core.Color" /> the values are copied from.</param>
public Color(Color color)
: this(color.APercent, color.RPercent, color.GPercent, color.BPercent)
{ }
private Color(byte a, byte r, byte g, byte b, double aP, double rP, double gP, double bP)
{
this.A = a;
this.R = r;
this.G = g;
this.B = b;
this.APercent = aP;
this.RPercent = rP;
this.GPercent = gP;
this.BPercent = bP;
}
#endregion
#region Methods
/// <summary>
/// Converts the individual byte values of this <see cref="Color"/> to a human-readable string.
/// </summary>
/// <returns>A string that contains the individual byte values of this <see cref="Color"/>. For example "[A: 255, R: 255, G: 0, B: 0]".</returns>
public override string ToString() => $"[A: {A}, R: {R}, G: {G}, B: {B}]";
/// <summary>
/// Tests whether the specified object is a <see cref="Color" /> and is equivalent to this <see cref="Color" />.
/// </summary>
/// <param name="obj">The object to test.</param>
/// <returns><c>true</c> if <paramref name="obj" /> is a <see cref="Color" /> equivalent to this <see cref="Color" />; otherwise, <c>false</c>.</returns>
public override bool Equals(object obj)
{
if (!(obj is Color)) return false;
(byte a, byte r, byte g, byte b) = (Color)obj;
return (a == A) && (r == R) && (g == G) && (b == B);
}
/// <summary>
/// Returns a hash code for this <see cref="Color" />.
/// </summary>
/// <returns>An integer value that specifies the hash code for this <see cref="Color" />.</returns>
public override int GetHashCode()
{
unchecked
{
int hashCode = APercent.GetHashCode();
hashCode = (hashCode * 397) ^ RPercent.GetHashCode();
hashCode = (hashCode * 397) ^ GPercent.GetHashCode();
hashCode = (hashCode * 397) ^ BPercent.GetHashCode();
return hashCode;
}
}
#region Deconstruction
/// <summary>
/// Deconstructs the Color into it's ARGB-components.
/// </summary>
/// <param name="a">The alpha component of this color.</param>
/// <param name="r">The red component of this color.</param>
/// <param name="g">The green component of this color.</param>
/// <param name="b">The blue component of this color.</param>
public void Deconstruct(out byte a, out byte r, out byte g, out byte b)
{
a = A;
r = R;
g = G;
b = B;
}
#endregion
#region Manipulation
/// <summary>
/// Blends a <see cref="Color"/> over this color.
/// </summary>
/// <param name="color">The <see cref="Color"/> to blend.</param>
public Color Blend(Color color)
{
if (color.A == 0) return this;
if (color.A == 255)
return color;
double resultA = (1.0 - ((1.0 - color.APercent) * (1.0 - APercent)));
double resultR = (((color.RPercent * color.APercent) / resultA) + ((RPercent * APercent * (1.0 - color.APercent)) / resultA));
double resultG = (((color.GPercent * color.APercent) / resultA) + ((GPercent * APercent * (1.0 - color.APercent)) / resultA));
double resultB = (((color.BPercent * color.APercent) / resultA) + ((BPercent * APercent * (1.0 - color.APercent)) / resultA));
return new Color(resultA, resultR, resultG, resultB);
}
#endregion
#endregion
#region Operators
/// <summary>
/// Blends the provided colors as if <see cref="Blend"/> would've been called on <paramref name="color1" />.
/// </summary>
/// <param name="color1">The base color.</param>
/// <param name="color2">The color to blend.</param>
/// <returns>The blended color.</returns>
public static Color operator +(Color color1, Color color2) => color1.Blend(color2);
/// <summary>
/// Returns a value that indicates whether two specified <see cref="Color" /> are equal.
/// </summary>
/// <param name="color1">The first <see cref="Color" /> to compare.</param>
/// <param name="color2">The second <see cref="Color" /> to compare.</param>
/// <returns><c>true</c> if <paramref name="color1" /> and <paramref name="color2" /> are equal; otherwise, <c>false</c>.</returns>
public static bool operator ==(Color color1, Color color2) => color1.Equals(color2);
/// <summary>
/// Returns a value that indicates whether two specified <see cref="Color" /> are equal.
/// </summary>
/// <param name="color1">The first <see cref="Color" /> to compare.</param>
/// <param name="color2">The second <see cref="Color" /> to compare.</param>
/// <returns><c>true</c> if <paramref name="color1" /> and <paramref name="color2" /> are not equal; otherwise, <c>false</c>.</returns>
public static bool operator !=(Color color1, Color color2) => !(color1 == color2);
/// <summary>
/// Converts a <see cref="ValueTuple"/> of ARGB-components to a <see cref="Color"/>.
/// </summary>
/// <param name="components">The <see cref="ValueTuple"/> containing the components.</param>
/// <returns>The color.</returns>
public static implicit operator Color((byte a, byte r, byte g, byte b) components) => new Color(components.a, components.r, components.g, components.b);
/// <summary>
/// Converts a <see cref="ValueTuple"/> of HSV-components to a <see cref="Color"/>.
/// </summary>
/// <param name="components">The <see cref="ValueTuple"/> containing the components.</param>
/// <returns>The color.</returns>
public static implicit operator Color((double hue, double saturation, double value) components) => new Color(components.hue, components.saturation, components.value);
#endregion
}
}

View File

@ -0,0 +1,203 @@
using System;
namespace RGB.NET.Core
{
public static class HSVColor
{
#region Getter
public static double GetHue(this Color color) => color.GetHSV().hue;
public static double GetSaturation(this Color color) => color.GetHSV().saturation;
public static double GetValue(this Color color) => color.GetHSV().value;
public static (double hue, double saturation, double value) GetHSV(this Color color)
=> CaclulateHSVFromRGB(color.RPercent, color.GPercent, color.BPercent);
#endregion
#region Manipulation
/// <summary>
/// Adds the given HSV values to this color.
/// </summary>
/// <param name="hue">The hue value to add.</param>
/// <param name="saturation">The saturation value to add.</param>
/// <param name="value">The value value to add.</param>
/// <returns>The new color after the modification.</returns>
public static Color AddHSV(this Color color, double hue = 0, double saturation = 0, double value = 0)
{
(double cHue, double cSaturation, double cValue) = color.GetHSV();
return Create(color.APercent, cHue + hue, cSaturation + saturation, cValue + value);
}
/// <summary>
/// Subtracts the given HSV values to this color.
/// </summary>
/// <param name="hue">The hue value to subtract.</param>
/// <param name="saturation">The saturation value to subtract.</param>
/// <param name="value">The value value to subtract.</param>
/// <returns>The new color after the modification.</returns>
public static Color SubtractHSV(this Color color, double hue = 0, double saturation = 0, double value = 0)
{
(double cHue, double cSaturation, double cValue) = color.GetHSV();
return Create(color.APercent, cHue - hue, cSaturation - saturation, cValue - value);
}
/// <summary>
/// Multiplies the given HSV values to this color.
/// </summary>
/// <param name="hue">The hue value to multiply.</param>
/// <param name="saturation">The saturation value to multiply.</param>
/// <param name="value">The value value to multiply.</param>
/// <returns>The new color after the modification.</returns>
public static Color MultiplyHSV(this Color color, double hue = 1, double saturation = 1, double value = 1)
{
(double cHue, double cSaturation, double cValue) = color.GetHSV();
return Create(color.APercent, cHue * hue, cSaturation * saturation, cValue * value);
}
/// <summary>
/// Divides the given HSV values to this color.
/// </summary>
/// <param name="hue">The hue value to divide.</param>
/// <param name="saturation">The saturation value to divide.</param>
/// <param name="value">The value value to divide.</param>
/// <returns>The new color after the modification.</returns>
public static Color DivideHSV(this Color color, double hue = 1, double saturation = 1, double value = 1)
{
(double cHue, double cSaturation, double cValue) = color.GetHSV();
return Create(color.APercent, cHue / hue, cSaturation / saturation, cValue / value);
}
/// <summary>
/// Sets the given hue value of this color.
/// </summary>
/// <param name="hue">The hue value to set.</param>
/// <param name="saturation">The saturation value to set.</param>
/// <param name="value">The value value to set.</param>
/// <returns>The new color after the modification.</returns>
public static Color SetHSV(this Color color, double? hue = null, double? saturation = null, double? value = null)
{
(double cHue, double cSaturation, double cValue) = color.GetHSV();
return Create(color.APercent, hue ?? cHue, saturation ?? cSaturation, value ?? cValue);
}
#endregion
#region Factory
/// <summary>
/// Creates a new instance of the <see cref="T:RGB.NET.Core.Color" /> struct using HSV-Values.
/// </summary>
/// <param name="hue">The hue component value of this <see cref="Color"/>.</param>
/// <param name="saturation">The saturation component value of this <see cref="Color"/>.</param>
/// <param name="value">The value component value of this <see cref="Color"/>.</param>
/// <returns>The color created from the values.</returns>
public static Color Create(double hue, double saturation, double value)
=> Create(1.0, hue, saturation, value);
/// <summary>
/// Creates a new instance of the <see cref="T:RGB.NET.Core.Color" /> struct using AHSV-Values.
/// </summary>
/// <param name="a">The alpha component value of this <see cref="Color"/>.</param>
/// <param name="hue">The hue component value of this <see cref="Color"/>.</param>
/// <param name="saturation">The saturation component value of this <see cref="Color"/>.</param>
/// <param name="value">The value component value of this <see cref="Color"/>.</param>
/// <returns>The color created from the values.</returns>
public static Color Create(byte a, double hue, double saturation, double value)
=> Create((double)a / byte.MaxValue, hue, saturation, value);
/// <summary>
/// Creates a new instance of the <see cref="T:RGB.NET.Core.Color" /> struct using AHSV-Values.
/// </summary>
/// <param name="a">The alpha component value of this <see cref="Color"/>.</param>
/// <param name="hue">The hue component value of this <see cref="Color"/>.</param>
/// <param name="saturation">The saturation component value of this <see cref="Color"/>.</param>
/// <param name="value">The value component value of this <see cref="Color"/>.</param>
/// <returns>The color created from the values.</returns>
public static Color Create(int a, double hue, double saturation, double value)
=> Create((double)a / byte.MaxValue, hue, saturation, value);
/// <summary>
/// Creates a new instance of the <see cref="T:RGB.NET.Core.Color" /> struct using AHSV-Values.
/// </summary>
/// <param name="a">The alpha component value of this <see cref="Color"/>.</param>
/// <param name="hue">The hue component value of this <see cref="Color"/>.</param>
/// <param name="saturation">The saturation component value of this <see cref="Color"/>.</param>
/// <param name="value">The value component value of this <see cref="Color"/>.</param>
/// <returns>The color created from the values.</returns>
public static Color Create(double a, double hue, double saturation, double value)
{
(double r, double g, double b) = CalculateRGBFromHSV(hue, saturation, value);
return new Color(a, r, g, b);
}
#endregion
#region Helper
private static (double h, double s, double v) CaclulateHSVFromRGB(double r, double g, double b)
{
if ((r == g) && (g == b)) return (0, 0, r);
double min = Math.Min(Math.Min(r, g), b);
double max = Math.Max(Math.Max(r, g), b);
double hue;
if (max == min)
hue = 0;
else if (max == r) // r is max
hue = (g - b) / (max - min);
else if (max == g) // g is max
hue = 2.0 + ((b - r) / (max - min));
else // b is max
hue = 4.0 + ((r - g) / (max - min));
hue = hue * 60.0;
hue = hue.Wrap(0, 360);
double saturation = (max == 0) ? 0 : 1.0 - (min / max);
double value = Math.Max(r, Math.Max(g, b));
return (hue, saturation, value);
}
private static (double r, double g, double b) CalculateRGBFromHSV(double h, double s, double v)
{
h = h.Wrap(0, 360);
s = s.Clamp(0, 1);
v = v.Clamp(0, 1);
if (s <= 0.0)
return (v, v, v);
double hh = h / 60.0;
int i = (int)hh;
double ff = hh - i;
double p = v * (1.0 - s);
double q = v * (1.0 - (s * ff));
double t = v * (1.0 - (s * (1.0 - ff)));
switch (i)
{
case 0:
return (v, t, p);
case 1:
return (q, v, p);
case 2:
return (p, v, t);
case 3:
return (p, q, v);
case 4:
return (t, p, v);
default:
return (v, p, q);
}
}
#endregion
}
}

View File

@ -0,0 +1,232 @@
using System;
namespace RGB.NET.Core
{
public static class RGBColor
{
#region Getter
public static (double r, double g, double b) GetRGBPercent(this Color color)
=> (color.RPercent, color.GPercent, color.BPercent);
#endregion
#region Manipulation
#region Add
/// <summary>
/// Adds the given RGB values to this color.
/// </summary>
/// <param name="r">The red value to add.</param>
/// <param name="g">The green value to add.</param>
/// <param name="b">The blue value to add.</param>
/// <returns>The new color after the modification.</returns>
public static Color AddRGB(this Color color, int r = 0, int g = 0, int b = 0)
=> new Color(color.APercent, color.R + r, color.G + g, color.B + b);
/// <summary>
/// Adds the given RGB-percent values to this color.
/// </summary>
/// <param name="r">The red value to add.</param>
/// <param name="g">The green value to add.</param>
/// <param name="b">The blue value to add.</param>
/// <returns>The new color after the modification.</returns>
public static Color AddRGB(this Color color, double r = 0, double g = 0, double b = 0)
=> new Color(color.APercent, color.RPercent + r, color.GPercent + g, color.BPercent + b);
/// <summary>
/// Adds the given alpha value to this color.
/// </summary>
/// <param name="a">The alpha value to add.</param>
/// <returns>The new color after the modification.</returns>
public static Color AddA(this Color color, int a)
=> new Color(color.A + a, color.RPercent, color.GPercent, color.BPercent);
/// <summary>
/// Adds the given alpha-percent value to this color.
/// </summary>
/// <param name="a">The alpha value to add.</param>
/// <returns>The new color after the modification.</returns>
public static Color AddA(this Color color, double a)
=> new Color(color.APercent + a, color.RPercent, color.GPercent, color.BPercent);
#endregion
#region Subtract
/// <summary>
/// Subtracts the given RGB values to this color.
/// </summary>
/// <param name="r">The red value to subtract.</param>
/// <param name="g">The green value to subtract.</param>
/// <param name="b">The blue value to subtract.</param>
/// <returns>The new color after the modification.</returns>
public static Color SubtractRGB(this Color color, int r = 0, int g = 0, int b = 0)
=> new Color(color.APercent, color.R - r, color.G - g, color.B - b);
/// <summary>
/// Subtracts the given RGB values to this color.
/// </summary>
/// <param name="r">The red value to subtract.</param>
/// <param name="g">The green value to subtract.</param>
/// <param name="b">The blue value to subtract.</param>
/// <returns>The new color after the modification.</returns>
public static Color SubtractRGB(this Color color, double r = 0, double g = 0, double b = 0)
=> new Color(color.APercent, color.RPercent - r, color.GPercent - g, color.BPercent - b);
/// <summary>
/// Subtracts the given alpha value to this color.
/// </summary>
/// <param name="a">The alpha value to subtract.</param>
/// <returns>The new color after the modification.</returns>
public static Color SubtractA(this Color color, int a)
=> new Color(color.A - a, color.RPercent, color.GPercent, color.BPercent);
/// <summary>
/// Subtracts the given alpha-percent value to this color.
/// </summary>
/// <param name="a">The alpha value to subtract.</param>
/// <returns>The new color after the modification.</returns>
public static Color SubtractA(this Color color, double aPercent)
=> new Color(color.APercent - aPercent, color.RPercent, color.GPercent, color.BPercent);
#endregion
#region Multiply
/// <summary>
/// Multiplies the given RGB values to this color.
/// </summary>
/// <param name="r">The red value to multiply.</param>
/// <param name="g">The green value to multiply.</param>
/// <param name="b">The blue value to multiply.</param>
/// <returns>The new color after the modification.</returns>
public static Color MultiplyRGB(this Color color, double r = 1, double g = 1, double b = 1)
=> new Color(color.APercent, color.RPercent * r, color.GPercent * g, color.BPercent * b);
/// <summary>
/// Multiplies the given alpha value to this color.
/// </summary>
/// <param name="a">The alpha value to multiply.</param>
/// <returns>The new color after the modification.</returns>
public static Color MultiplyA(this Color color, double a)
=> new Color(color.APercent * a, color.RPercent, color.GPercent, color.BPercent);
#endregion
#region Divide
/// <summary>
/// Divides the given RGB values to this color.
/// </summary>
/// <param name="r">The red value to divide.</param>
/// <param name="g">The green value to divide.</param>
/// <param name="b">The blue value to divide.</param>
/// <returns>The new color after the modification.</returns>
public static Color DivideRGB(this Color color, double r = 1, double g = 1, double b = 1)
=> new Color(color.APercent, color.RPercent / r, color.GPercent / g, color.BPercent / b);
/// <summary>
/// Divides the given alpha value to this color.
/// </summary>
/// <param name="a">The alpha value to divide.</param>
/// <returns>The new color after the modification.</returns>
public static Color DivideA(this Color color, double a)
=> new Color(color.APercent / a, color.RPercent, color.GPercent, color.BPercent);
#endregion
#region Set
/// <summary>
/// Sets the given RGB value of this color.
/// </summary>
/// <param name="r">The red value to set.</param>
/// <param name="g">The green value to set.</param>
/// <param name="b">The blue value to set.</param>
/// <returns>The new color after the modification.</returns>
public static Color SetRGB(this Color color, byte? r = null, byte? g = null, byte? b = null)
=> new Color(color.APercent, r ?? color.R, g ?? color.G, b ?? color.B);
/// <summary>
/// Sets the given RGB value of this color.
/// </summary>
/// <param name="r">The red value to set.</param>
/// <param name="g">The green value to set.</param>
/// <param name="b">The blue value to set.</param>
/// <returns>The new color after the modification.</returns>
public static Color SetRGB(this Color color, int? r = null, int? g = null, int? b = null)
=> new Color(color.APercent, r ?? color.R, g ?? color.G, b ?? color.B);
/// <summary>
/// Sets the given RGB value of this color.
/// </summary>
/// <param name="r">The red value to set.</param>
/// <param name="g">The green value to set.</param>
/// <param name="b">The blue value to set.</param>
/// <returns>The new color after the modification.</returns>
public static Color SetRGB(this Color color, double? r = null, double? g = null, double? b = null)
=> new Color(color.APercent, r ?? color.RPercent, g ?? color.GPercent, b ?? color.BPercent);
/// <summary>
/// Sets the given alpha value of this color.
/// </summary>
/// <param name="a">The alpha value to set.</param>
/// <returns>The new color after the modification.</returns>
public static Color SetA(this Color color, int a) => new Color(a, color.RPercent, color.GPercent, color.BPercent);
/// <summary>
/// Sets the given alpha value of this color.
/// </summary>
/// <param name="a">The alpha value to set.</param>
/// <returns>The new color after the modification.</returns>
public static Color SetA(this Color color, double a) => new Color(a, color.RPercent, color.GPercent, color.BPercent);
#endregion
#endregion
#region Conversion
/// <summary>
/// Gets the current color as a RGB-HEX-string.
/// </summary>
/// <returns>The RGB-HEX-string.</returns>
public static string AsRGBHexString(this Color color, bool leadingHash = true) => (leadingHash ? "#" : "") + ConversionHelper.ToHex(color.R, color.G, color.B);
/// <summary>
/// Gets the current color as a ARGB-HEX-string.
/// </summary>
/// <returns>The ARGB-HEX-string.</returns>
public static string AsARGBHexString(this Color color, bool leadingHash = true) => (leadingHash ? "#" : "") + ConversionHelper.ToHex(color.A, color.R, color.G, color.B);
#endregion
#region Factory
/// <summary>
/// Creates a new instance of the <see cref="T:RGB.NET.Core.Color" /> struct using a HEX-string.
/// </summary>
/// <param name="hexString">The HEX-representation of the color.</param>
/// <returns>The color created from the HEX-string.</returns>
public static Color FromHexString(string hexString)
{
if ((hexString == null) || (hexString.Length < 6))
throw new ArgumentException("Invalid hex string", nameof(hexString));
if (hexString[0] == '#')
hexString = hexString.Substring(1);
byte[] data = ConversionHelper.HexToBytes(hexString);
if (data.Length == 3)
return new Color(data[0], data[1], data[2]);
if (data.Length == 4)
return new Color(data[0], data[1], data[2], data[3]);
throw new ArgumentException("Invalid hex string", nameof(hexString));
}
#endregion
}
}

View File

@ -36,7 +36,14 @@ namespace RGB.NET.Core
/// <param name="max">The higher value of the range the value is clamped to.</param>
/// <returns>The clamped value.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double Clamp(this double value, double min, double max) => Math.Max(min, Math.Min(max, value));
public static double Clamp(this double value, double min, double max)
{
// ReSharper disable ConvertIfStatementToReturnStatement - I'm not sure why, but inlining this statement reduces performance by ~10%
if (value < min) return min;
if (value > max) return max;
return value;
// ReSharper restore ConvertIfStatementToReturnStatement
}
/// <summary>
/// Clamps the provided value to be bigger or equal min and smaller or equal max.
@ -46,7 +53,14 @@ namespace RGB.NET.Core
/// <param name="max">The higher value of the range the value is clamped to.</param>
/// <returns>The clamped value.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int Clamp(this int value, int min, int max) => Math.Max(min, Math.Min(max, value));
public static int Clamp(this int value, int min, int max)
{
// ReSharper disable ConvertIfStatementToReturnStatement - I'm not sure why, but inlining this statement reduces performance by ~10%
if (value < min) return min;
if (value > max) return max;
return value;
// ReSharper restore ConvertIfStatementToReturnStatement
}
/// <summary>
/// Enforces the provided value to be in the specified range by wrapping it around the edges if it exceeds them.
@ -78,6 +92,10 @@ namespace RGB.NET.Core
return (byte)(percentage >= 1.0 ? 255 : percentage * 256.0);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double GetPercentageFromByteValue(this byte value)
=> ((double)value) / byte.MaxValue;
#endregion
}
}

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,6 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=brushes/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=color/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=colorcorrection/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=decorators/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=devices/@EntryIndexedValue">True</s:Boolean>

View File

@ -77,7 +77,7 @@ namespace RGB.NET.Decorators.Brush
#region Methods
/// <inheritdoc />
public Color ManipulateColor(Rectangle rectangle, BrushRenderTarget renderTarget, Color color) => color.SetAPercent(_currentValue);
public Color ManipulateColor(Rectangle rectangle, BrushRenderTarget renderTarget, Color color) => color.SetA(_currentValue);
/// <inheritdoc />
protected override void Update(double deltaTime)

View File

@ -40,14 +40,16 @@ namespace RGB.NET.Devices.Novation
/// <returns>The novation-representation of the <see cref="Color"/>.</returns>
protected virtual int ConvertColor(Color color)
{
if ((color.Hue >= 330) || (color.Hue < 30))
return (int)Math.Ceiling(color.Value * 3); // red with brightness 1, 2 or 3
(double hue, double saturation, double value) = color.GetHSV();
if ((color.Hue >= 30) && (color.Hue < 90)) // yellow with brightness 17, 34 or 51
return (int)Math.Ceiling(color.Value * 3) * 17;
if ((hue >= 330) || (hue < 30))
return (int)Math.Ceiling(value * 3); // red with brightness 1, 2 or 3
if ((color.Hue >= 90) && (color.Hue < 150)) // green with brightness 16, 32 or 48
return (int)Math.Ceiling(color.Value * 3) * 16;
if ((hue >= 30) && (hue < 90)) // yellow with brightness 17, 34 or 51
return (int)Math.Ceiling(value * 3) * 17;
if ((hue >= 90) && (hue < 150)) // green with brightness 16, 32 or 48
return (int)Math.Ceiling(value * 3) * 16;
return 0;
}

View File

@ -66,7 +66,7 @@ namespace RGB.NET.Devices.SoIP.Client
List<(LedId, Color)> leds = message.MessageString.Split(';').Select(x =>
{
string[] led = x.Split('|');
return ((LedId)Enum.Parse(typeof(LedId), led[0]), Color.FromHexString(led[1]));
return ((LedId)Enum.Parse(typeof(LedId), led[0]), RGBColor.FromHexString(led[1]));
}).ToList();
lock (_syncbackCache)
foreach ((LedId ledId, Color color) in leds)

View File

@ -61,7 +61,7 @@ namespace RGB.NET.Devices.SoIP.Server
tcpClient.GetStream().WriteAsync(messageData, 0, messageData.Length);
}
private string GetLedString(IEnumerable<Led> leds) => string.Join(";", leds.Select(x => x.Id.ToString() + "|" + x.Color.AsARGBHexString()));
private string GetLedString(IEnumerable<Led> leds) => string.Join(";", leds.Select(x => x.Id.ToString() + "|" + x.Color.AsARGBHexString(false)));
/// <inheritdoc />
public override void Dispose()

View File

@ -46,7 +46,7 @@ namespace RGB.NET.Devices.SoIP.Server
}
}
private string GetLedString(Dictionary<object, Color> dataSet) => string.Join(";", dataSet.Select(x => x.Key.ToString() + "|" + x.Value.AsARGBHexString()));
private string GetLedString(Dictionary<object, Color> dataSet) => string.Join(";", dataSet.Select(x => x.Key.ToString() + "|" + x.Value.AsARGBHexString(false)));
#endregion
}

View File

@ -39,7 +39,7 @@ namespace RGB.NET.Devices.WS281X.Bitwizard
protected override IEnumerable<string> GetCommands(Dictionary<object, Color> dataSet)
{
foreach (KeyValuePair<object, Color> data in dataSet)
yield return $"pix {(int)data.Key} {data.Value.AsRGBHexString()}";
yield return $"pix {(int)data.Key} {data.Value.AsRGBHexString(false)}";
}
#endregion

View File

@ -6,85 +6,61 @@ namespace RGB.NET.Core.Tests.Color
public class HSVColorTest
{
#region Manipulation
#region Blend
[TestMethod]
public void BlendOpaqueTest()
{
Assert.Inconclusive();
//Core.Color baseColor = new Core.Color(255, 0, 0);
//Core.Color blendColor = new Core.Color(0, 255, 0);
//Assert.AreEqual(blendColor, baseColor.Blend(blendColor));
}
[TestMethod]
public void BlendTransparentTest()
{
Assert.Inconclusive();
//Core.Color baseColor = new Core.Color(255, 0, 0);
//Core.Color blendColor = new Core.Color(0, 0, 255, 0);
//Assert.AreEqual(baseColor, baseColor.Blend(blendColor));
}
#endregion
#region Add
[TestMethod]
public void AddHueTest()
{
Core.Color baseColor = Core.Color.FromHSV(180, 0.5, 0.5);
Core.Color result = baseColor.AddHue(30);
Assert.AreEqual(Core.Color.FromHSV(210, 0.5, 0.5), result);
Core.Color baseColor = HSVColor.Create(180, 0.5, 0.5);
Core.Color result = baseColor.AddHSV(hue: 30);
Assert.AreEqual(HSVColor.Create(210, 0.5, 0.5), result);
}
[TestMethod]
public void AddHueWrapTest()
{
Core.Color baseColor = Core.Color.FromHSV(180, 0.5, 0.5);
Core.Color result = baseColor.AddHue(220);
Core.Color baseColor = HSVColor.Create(180, 0.5, 0.5);
Core.Color result = baseColor.AddHSV(hue: 220);
Assert.AreEqual(Core.Color.FromHSV(40, 0.5, 0.5), result);
Assert.AreEqual(HSVColor.Create(40, 0.5, 0.5), result);
}
[TestMethod]
public void AddSaturationTest()
{
Core.Color baseColor = Core.Color.FromHSV(180, 0.5, 0.5);
Core.Color result = baseColor.AddSaturation(0.3);
Core.Color baseColor = HSVColor.Create(180, 0.5, 0.5);
Core.Color result = baseColor.AddHSV(saturation: 0.3);
Assert.AreEqual(Core.Color.FromHSV(180, 0.8, 0.5), result);
Assert.AreEqual(HSVColor.Create(180, 0.8, 0.5), result);
}
[TestMethod]
public void AddSaturationClampTest()
{
Core.Color baseColor = Core.Color.FromHSV(180, 0.5, 0.5);
Core.Color result = baseColor.AddSaturation(0.8);
Core.Color baseColor = HSVColor.Create(180, 0.5, 0.5);
Core.Color result = baseColor.AddHSV(saturation: 0.8);
Assert.AreEqual(Core.Color.FromHSV(180, 1.0, 0.5), result);
Assert.AreEqual(HSVColor.Create(180, 1.0, 0.5), result);
}
[TestMethod]
public void AddValueTest()
{
Core.Color baseColor = Core.Color.FromHSV(180, 0.5, 0.5);
Core.Color result = baseColor.AddValue(0.3);
Core.Color baseColor = HSVColor.Create(180, 0.5, 0.5);
Core.Color result = baseColor.AddHSV(value: 0.3);
Assert.AreEqual(Core.Color.FromHSV(180, 0.5, 0.8), result);
Assert.AreEqual(HSVColor.Create(180, 0.5, 0.8), result);
}
[TestMethod]
public void AddValueClampTest()
{
Core.Color baseColor = Core.Color.FromHSV(180, 0.5, 0.5);
Core.Color result = baseColor.AddValue(0.8);
Core.Color baseColor = HSVColor.Create(180, 0.5, 0.5);
Core.Color result = baseColor.AddHSV(value: 0.8);
Assert.AreEqual(Core.Color.FromHSV(180, 0.5, 1.0), result);
Assert.AreEqual(HSVColor.Create(180, 0.5, 1.0), result);
}
#endregion
@ -94,55 +70,55 @@ namespace RGB.NET.Core.Tests.Color
[TestMethod]
public void SubtractHueTest()
{
Core.Color baseColor = Core.Color.FromHSV(180, 0.5, 0.5);
Core.Color result = baseColor.SubtractHue(30);
Core.Color baseColor = HSVColor.Create(180, 0.5, 0.5);
Core.Color result = baseColor.SubtractHSV(hue: 30);
Assert.AreEqual(Core.Color.FromHSV(150, 0.5, 0.5), result);
Assert.AreEqual(HSVColor.Create(150, 0.5, 0.5), result);
}
[TestMethod]
public void SubtractHueWrapTest()
{
Core.Color baseColor = Core.Color.FromHSV(180, 0.5, 0.5);
Core.Color result = baseColor.SubtractHue(220);
Core.Color baseColor = HSVColor.Create(180, 0.5, 0.5);
Core.Color result = baseColor.SubtractHSV(hue: 220);
Assert.AreEqual(Core.Color.FromHSV(320, 0.5, 0.5), result);
Assert.AreEqual(HSVColor.Create(320, 0.5, 0.5), result);
}
[TestMethod]
public void SubtractSaturationTest()
{
Core.Color baseColor = Core.Color.FromHSV(180, 0.5, 0.5);
Core.Color result = baseColor.SubtractSaturation(0.3);
Core.Color baseColor = HSVColor.Create(180, 0.5, 0.5);
Core.Color result = baseColor.SubtractHSV(saturation: 0.3);
Assert.AreEqual(Core.Color.FromHSV(180, 0.2, 0.5), result);
Assert.AreEqual(HSVColor.Create(180, 0.2, 0.5), result);
}
[TestMethod]
public void SubtractSaturationClampTest()
{
Core.Color baseColor = Core.Color.FromHSV(180, 0.5, 0.5);
Core.Color result = baseColor.SubtractSaturation(0.8);
Core.Color baseColor = HSVColor.Create(180, 0.5, 0.5);
Core.Color result = baseColor.SubtractHSV(saturation: 0.8);
Assert.AreEqual(Core.Color.FromHSV(180, 0, 0.5), result);
Assert.AreEqual(HSVColor.Create(180, 0, 0.5), result);
}
[TestMethod]
public void SubtractValueTest()
{
Core.Color baseColor = Core.Color.FromHSV(180, 0.5, 0.5);
Core.Color result = baseColor.SubtractValue(0.3);
Core.Color baseColor = HSVColor.Create(180, 0.5, 0.5);
Core.Color result = baseColor.SubtractHSV(value: 0.3);
Assert.AreEqual(Core.Color.FromHSV(180, 0.5, 0.2), result);
Assert.AreEqual(HSVColor.Create(180, 0.5, 0.2), result);
}
[TestMethod]
public void SubtractValueClampTest()
{
Core.Color baseColor = Core.Color.FromHSV(180, 0.5, 0.5);
Core.Color result = baseColor.SubtractValue(0.8);
Core.Color baseColor = HSVColor.Create(180, 0.5, 0.5);
Core.Color result = baseColor.SubtractHSV(value: .8);
Assert.AreEqual(Core.Color.FromHSV(180, 0.5, 0), result);
Assert.AreEqual(HSVColor.Create(180, 0.5, 0), result);
}
#endregion
@ -152,55 +128,55 @@ namespace RGB.NET.Core.Tests.Color
[TestMethod]
public void MultiplyHueTest()
{
Core.Color baseColor = Core.Color.FromHSV(180, 0.5, 0.5);
Core.Color result = baseColor.MultiplyHue(1.5);
Core.Color baseColor = HSVColor.Create(180, 0.5, 0.5);
Core.Color result = baseColor.MultiplyHSV(hue: 1.5);
Assert.AreEqual(Core.Color.FromHSV(270, 0.5, 0.5), result);
Assert.AreEqual(HSVColor.Create(270, 0.5, 0.5), result);
}
[TestMethod]
public void MultiplyHueWrapTest()
{
Core.Color baseColor = Core.Color.FromHSV(180, 0.5, 0.5);
Core.Color result = baseColor.MultiplyHue(3);
Core.Color baseColor = HSVColor.Create(180, 0.5, 0.5);
Core.Color result = baseColor.MultiplyHSV(hue: 3);
Assert.AreEqual(Core.Color.FromHSV(180, 0.5, 0.5), result);
Assert.AreEqual(HSVColor.Create(180, 0.5, 0.5), result);
}
[TestMethod]
public void MultiplySaturationTest()
{
Core.Color baseColor = Core.Color.FromHSV(180, 0.2, 0.2);
Core.Color result = baseColor.MultiplySaturation(3);
Core.Color baseColor = HSVColor.Create(180, 0.2, 0.2);
Core.Color result = baseColor.MultiplyHSV(saturation: 3);
Assert.AreEqual(Core.Color.FromHSV(180, 0.6, 0.2), result);
Assert.AreEqual(HSVColor.Create(180, 0.6, 0.2), result);
}
[TestMethod]
public void MultiplySaturationClampTest()
{
Core.Color baseColor = Core.Color.FromHSV(180, 0.5, 0.5);
Core.Color result = baseColor.MultiplySaturation(3);
Core.Color baseColor = HSVColor.Create(180, 0.5, 0.5);
Core.Color result = baseColor.MultiplyHSV(saturation: 3);
Assert.AreEqual(Core.Color.FromHSV(180, 1.0, 0.5), result);
Assert.AreEqual(HSVColor.Create(180, 1.0, 0.5), result);
}
[TestMethod]
public void MultiplyValueTest()
{
Core.Color baseColor = Core.Color.FromHSV(180, 0.2, 0.2);
Core.Color result = baseColor.MultiplyValue(3);
Core.Color baseColor = HSVColor.Create(180, 0.2, 0.2);
Core.Color result = baseColor.MultiplyHSV(value: 3);
Assert.AreEqual(Core.Color.FromHSV(180, 0.2, 0.6), result);
Assert.AreEqual(HSVColor.Create(180, 0.2, 0.6), result);
}
[TestMethod]
public void MultiplyValueClampTest()
{
Core.Color baseColor = Core.Color.FromHSV(180, 0.5, 0.5);
Core.Color result = baseColor.MultiplyValue(3);
Core.Color baseColor = HSVColor.Create(180, 0.5, 0.5);
Core.Color result = baseColor.MultiplyHSV(value: 3);
Assert.AreEqual(Core.Color.FromHSV(180, 0.5, 1.0), result);
Assert.AreEqual(HSVColor.Create(180, 0.5, 1.0), result);
}
#endregion
@ -210,28 +186,28 @@ namespace RGB.NET.Core.Tests.Color
[TestMethod]
public void DivideHueTest()
{
Core.Color baseColor = Core.Color.FromHSV(180, 0.5, 0.5);
Core.Color result = baseColor.DivideHue(30);
Core.Color baseColor = HSVColor.Create(180, 0.5, 0.5);
Core.Color result = baseColor.DivideHSV(hue: 30);
Assert.AreEqual(Core.Color.FromHSV(6, 0.5, 0.5), result);
Assert.AreEqual(HSVColor.Create(6, 0.5, 0.5), result);
}
[TestMethod]
public void DivideSaturationTest()
{
Core.Color baseColor = Core.Color.FromHSV(180, 0.6, 0.6);
Core.Color result = baseColor.DivideSaturation(2);
Core.Color baseColor = HSVColor.Create(180, 0.6, 0.6);
Core.Color result = baseColor.DivideHSV(saturation: 2);
Assert.AreEqual(Core.Color.FromHSV(180, 0.3, 0.6), result);
Assert.AreEqual(HSVColor.Create(180, 0.3, 0.6), result);
}
[TestMethod]
public void DivideValueTest()
{
Core.Color baseColor = Core.Color.FromHSV(180, 0.6, 0.6);
Core.Color result = baseColor.DivideValue(2);
Core.Color baseColor = HSVColor.Create(180, 0.6, 0.6);
Core.Color result = baseColor.DivideHSV(value: 2);
Assert.AreEqual(Core.Color.FromHSV(180, 0.6, 0.3), result);
Assert.AreEqual(HSVColor.Create(180, 0.6, 0.3), result);
}
#endregion
@ -241,64 +217,64 @@ namespace RGB.NET.Core.Tests.Color
[TestMethod]
public void SetHueTest()
{
Core.Color baseColor = Core.Color.FromHSV(180, 0.5, 0.5);
Core.Color result = baseColor.SetHue(30);
Core.Color baseColor = HSVColor.Create(180, 0.5, 0.5);
Core.Color result = baseColor.SetHSV(hue: 30);
Assert.AreEqual(Core.Color.FromHSV(30, 0.5, 0.5), result);
Assert.AreEqual(HSVColor.Create(30, 0.5, 0.5), result);
}
[TestMethod]
public void SetHueWrapTest()
{
Core.Color baseColor = Core.Color.FromHSV(180, 0.5, 0.5);
Core.Color result = baseColor.SetHue(440);
Core.Color baseColor = HSVColor.Create(180, 0.5, 0.5);
Core.Color result = baseColor.SetHSV(hue: 440);
Assert.AreEqual(Core.Color.FromHSV(80, 0.5, 0.5), result);
Assert.AreEqual(HSVColor.Create(80, 0.5, 0.5), result);
}
[TestMethod]
public void SetHueWrapNegativeTest()
{
Core.Color baseColor = Core.Color.FromHSV(180, 0.5, 0.5);
Core.Color result = baseColor.SetHue(-30);
Core.Color baseColor = HSVColor.Create(180, 0.5, 0.5);
Core.Color result = baseColor.SetHSV(hue: -30);
Assert.AreEqual(Core.Color.FromHSV(330, 0.5, 0.5), result);
Assert.AreEqual(HSVColor.Create(330, 0.5, 0.5), result);
}
[TestMethod]
public void SetSaturationTest()
{
Core.Color baseColor = Core.Color.FromHSV(180, 0.5, 0.5);
Core.Color result = baseColor.SetSaturation(0.3);
Core.Color baseColor = HSVColor.Create(180, 0.5, 0.5);
Core.Color result = baseColor.SetHSV(saturation: 0.3);
Assert.AreEqual(Core.Color.FromHSV(180, 0.3, 0.5), result);
Assert.AreEqual(HSVColor.Create(180, 0.3, 0.5), result);
}
[TestMethod]
public void SetSaturationClampTest()
{
Core.Color baseColor = Core.Color.FromHSV(180, 0.5, 0.5);
Core.Color result = baseColor.SetSaturation(2);
Core.Color baseColor = HSVColor.Create(180, 0.5, 0.5);
Core.Color result = baseColor.SetHSV(saturation: 2);
Assert.AreEqual(Core.Color.FromHSV(180, 1.0, 0.5), result);
Assert.AreEqual(HSVColor.Create(180, 1.0, 0.5), result);
}
[TestMethod]
public void SetValueTest()
{
Core.Color baseColor = Core.Color.FromHSV(180, 0.5, 0.5);
Core.Color result = baseColor.SetValue(0.3);
Core.Color baseColor = HSVColor.Create(180, 0.5, 0.5);
Core.Color result = baseColor.SetHSV(value: 0.3);
Assert.AreEqual(Core.Color.FromHSV(180, 0.5, 0.3), result);
Assert.AreEqual(HSVColor.Create(180, 0.5, 0.3), result);
}
[TestMethod]
public void SetValueClampTest()
{
Core.Color baseColor = Core.Color.FromHSV(180, 0.5, 0.5);
Core.Color result = baseColor.SetValue(2);
Core.Color baseColor = HSVColor.Create(180, 0.5, 0.5);
Core.Color result = baseColor.SetHSV(value: 2);
Assert.AreEqual(Core.Color.FromHSV(180, 0.5, 1.0), result);
Assert.AreEqual(HSVColor.Create(180, 0.5, 1.0), result);
}
#endregion

View File

@ -40,7 +40,7 @@ namespace RGB.NET.Core.Tests.Color
public void BlendDownTest()
{
Core.Color baseColor = new Core.Color(1.0, 1.0, 1.0);
Core.Color blendColor = new Core.Color(1.0, 0.0, 0.0, 0.0);
Core.Color blendColor = new Core.Color(0.5, 0.0, 0.0, 0.0);
Assert.AreEqual(new Core.Color(0.5, 0.5, 0.5), baseColor.Blend(blendColor));
}
@ -58,33 +58,15 @@ namespace RGB.NET.Core.Tests.Color
Assert.AreEqual(new Core.Color(128, 139, 140, 141), result);
}
[TestMethod]
public void AddARGBTest()
{
Core.Color baseColor = new Core.Color(128, 128, 128, 128);
Core.Color result = baseColor.AddRGB(10, 11, 12, 13);
Assert.AreEqual(new Core.Color(138, 139, 140, 141), result);
}
[TestMethod]
public void AddRGBPercentTest()
{
Core.Color baseColor = new Core.Color(0.5, 0.5, 0.5, 0.5);
Core.Color result = baseColor.AddPercent(0.2, 0.3, 0.4);
Core.Color result = baseColor.AddRGB(0.2, 0.3, 0.4);
Assert.AreEqual(new Core.Color(0.5, 0.7, 0.8, 0.9), result);
}
[TestMethod]
public void AddARGBPercentTest()
{
Core.Color baseColor = new Core.Color(0.5, 0.5, 0.5, 0.5);
Core.Color result = baseColor.AddPercent(0.1, 0.2, 0.3, 0.4);
Assert.AreEqual(new Core.Color(0.6, 0.7, 0.8, 0.9), result);
}
[TestMethod]
public void AddATest()
{
@ -98,7 +80,7 @@ namespace RGB.NET.Core.Tests.Color
public void AddAPercentTest()
{
Core.Color baseColor = new Core.Color(0.5, 0.5, 0.5, 0.5);
Core.Color result = baseColor.AddAPercent(0.1);
Core.Color result = baseColor.AddA(0.1);
Assert.AreEqual(new Core.Color(0.6, 0.5, 0.5, 0.5), result);
}
@ -107,7 +89,7 @@ namespace RGB.NET.Core.Tests.Color
public void AddRTest()
{
Core.Color baseColor = new Core.Color(128, 128, 128, 128);
Core.Color result = baseColor.AddR(10);
Core.Color result = baseColor.AddRGB(r: 10);
Assert.AreEqual(new Core.Color(128, 138, 128, 128), result);
}
@ -116,7 +98,7 @@ namespace RGB.NET.Core.Tests.Color
public void AddRPercentTest()
{
Core.Color baseColor = new Core.Color(0.5, 0.5, 0.5, 0.5);
Core.Color result = baseColor.AddRPercent(0.1);
Core.Color result = baseColor.AddRGB(r: 0.1);
Assert.AreEqual(new Core.Color(0.5, 0.6, 0.5, 0.5), result);
}
@ -125,7 +107,7 @@ namespace RGB.NET.Core.Tests.Color
public void AddGTest()
{
Core.Color baseColor = new Core.Color(128, 128, 128, 128);
Core.Color result = baseColor.AddG(10);
Core.Color result = baseColor.AddRGB(g: 10);
Assert.AreEqual(new Core.Color(128, 128, 138, 128), result);
}
@ -134,7 +116,7 @@ namespace RGB.NET.Core.Tests.Color
public void AddGPercentTest()
{
Core.Color baseColor = new Core.Color(0.5, 0.5, 0.5, 0.5);
Core.Color result = baseColor.AddGPercent(0.1);
Core.Color result = baseColor.AddRGB(g: 0.1);
Assert.AreEqual(new Core.Color(0.5, 0.5, 0.6, 0.5), result);
}
@ -143,7 +125,7 @@ namespace RGB.NET.Core.Tests.Color
public void AddBTest()
{
Core.Color baseColor = new Core.Color(128, 128, 128, 128);
Core.Color result = baseColor.AddB(10);
Core.Color result = baseColor.AddRGB(b: 10);
Assert.AreEqual(new Core.Color(128, 128, 128, 138), result);
}
@ -152,7 +134,7 @@ namespace RGB.NET.Core.Tests.Color
public void AddBPercentTest()
{
Core.Color baseColor = new Core.Color(0.5, 0.5, 0.5, 0.5);
Core.Color result = baseColor.AddBPercent(0.1);
Core.Color result = baseColor.AddRGB(b: 0.1);
Assert.AreEqual(new Core.Color(0.5, 0.5, 0.5, 0.6), result);
}
@ -170,33 +152,15 @@ namespace RGB.NET.Core.Tests.Color
Assert.AreEqual(new Core.Color(128, 117, 116, 115), result);
}
[TestMethod]
public void SubtractARGBTest()
{
Core.Color baseColor = new Core.Color(128, 128, 128, 128);
Core.Color result = baseColor.SubtractRGB(10, 11, 12, 13);
Assert.AreEqual(new Core.Color(118, 117, 116, 115), result);
}
[TestMethod]
public void SubtractRGBPercentTest()
{
Core.Color baseColor = new Core.Color(0.5, 0.5, 0.5, 0.5);
Core.Color result = baseColor.SubtractPercent(0.2, 0.3, 0.4);
Core.Color result = baseColor.SubtractRGB(0.2, 0.3, 0.4);
Assert.AreEqual(new Core.Color(0.5, 0.3, 0.2, 0.1), result);
}
[TestMethod]
public void SubtractARGBPercentTest()
{
Core.Color baseColor = new Core.Color(0.5, 0.5, 0.5, 0.5);
Core.Color result = baseColor.SubtractPercent(0.1, 0.2, 0.3, 0.4);
Assert.AreEqual(new Core.Color(0.4, 0.3, 0.2, 0.1), result);
}
[TestMethod]
public void SubtractATest()
{
@ -210,7 +174,7 @@ namespace RGB.NET.Core.Tests.Color
public void SubtractAPercentTest()
{
Core.Color baseColor = new Core.Color(0.5, 0.5, 0.5, 0.5);
Core.Color result = baseColor.SubtractAPercent(0.1);
Core.Color result = baseColor.SubtractA(0.1);
Assert.AreEqual(new Core.Color(0.4, 0.5, 0.5, 0.5), result);
}
@ -219,7 +183,7 @@ namespace RGB.NET.Core.Tests.Color
public void SubtractRTest()
{
Core.Color baseColor = new Core.Color(128, 128, 128, 128);
Core.Color result = baseColor.SubtractR(10);
Core.Color result = baseColor.SubtractRGB(r: 10);
Assert.AreEqual(new Core.Color(128, 118, 128, 128), result);
}
@ -228,7 +192,7 @@ namespace RGB.NET.Core.Tests.Color
public void SubtractRPercentTest()
{
Core.Color baseColor = new Core.Color(0.5, 0.5, 0.5, 0.5);
Core.Color result = baseColor.SubtractRPercent(0.1);
Core.Color result = baseColor.SubtractRGB(r: 0.1);
Assert.AreEqual(new Core.Color(0.5, 0.4, 0.5, 0.5), result);
}
@ -237,7 +201,7 @@ namespace RGB.NET.Core.Tests.Color
public void SubtractGTest()
{
Core.Color baseColor = new Core.Color(128, 128, 128, 128);
Core.Color result = baseColor.SubtractG(10);
Core.Color result = baseColor.SubtractRGB(g: 10);
Assert.AreEqual(new Core.Color(128, 128, 118, 128), result);
}
@ -246,7 +210,7 @@ namespace RGB.NET.Core.Tests.Color
public void SubtractGPercentTest()
{
Core.Color baseColor = new Core.Color(0.5, 0.5, 0.5, 0.5);
Core.Color result = baseColor.SubtractGPercent(0.1);
Core.Color result = baseColor.SubtractRGB(g: 0.1);
Assert.AreEqual(new Core.Color(0.5, 0.5, 0.4, 0.5), result);
}
@ -255,7 +219,7 @@ namespace RGB.NET.Core.Tests.Color
public void SubtractBTest()
{
Core.Color baseColor = new Core.Color(128, 128, 128, 128);
Core.Color result = baseColor.SubtractB(10);
Core.Color result = baseColor.SubtractRGB(b: 10);
Assert.AreEqual(new Core.Color(128, 128, 128, 118), result);
}
@ -264,7 +228,7 @@ namespace RGB.NET.Core.Tests.Color
public void SubtractBPercentTest()
{
Core.Color baseColor = new Core.Color(0.5, 0.5, 0.5, 0.5);
Core.Color result = baseColor.SubtractBPercent(0.1);
Core.Color result = baseColor.SubtractRGB(b: 0.1);
Assert.AreEqual(new Core.Color(0.5, 0.5, 0.5, 0.4), result);
}
@ -277,25 +241,16 @@ namespace RGB.NET.Core.Tests.Color
public void MultiplyRGBPercentTest()
{
Core.Color baseColor = new Core.Color(0.2, 0.2, 0.2, 0.2);
Core.Color result = baseColor.MultiplyPercent(3, 4, 5);
Core.Color result = baseColor.MultiplyRGB(3, 4, 5);
Assert.AreEqual(new Core.Color(0.2, 0.6, 0.8, 1.0), result);
}
[TestMethod]
public void MultiplyARGBPercentTest()
{
Core.Color baseColor = new Core.Color(0.2, 0.2, 0.2, 0.2);
Core.Color result = baseColor.MultiplyPercent(2, 3, 4, 5);
Assert.AreEqual(new Core.Color(0.4, 0.6, 0.8, 1.0), result);
}
[TestMethod]
public void MultiplyAPercentTest()
{
Core.Color baseColor = new Core.Color(0.2, 0.2, 0.2, 0.2);
Core.Color result = baseColor.MultiplyAPercent(3);
Core.Color result = baseColor.MultiplyA(3);
Assert.AreEqual(new Core.Color(0.6, 0.2, 0.2, 0.2), result);
}
@ -304,7 +259,7 @@ namespace RGB.NET.Core.Tests.Color
public void MultiplyRPercentTest()
{
Core.Color baseColor = new Core.Color(0.2, 0.2, 0.2, 0.2);
Core.Color result = baseColor.MultiplyRPercent(3);
Core.Color result = baseColor.MultiplyRGB(r: 3);
Assert.AreEqual(new Core.Color(0.2, 0.6, 0.2, 0.2), result);
}
@ -313,7 +268,7 @@ namespace RGB.NET.Core.Tests.Color
public void MultiplyGPercentTest()
{
Core.Color baseColor = new Core.Color(0.2, 0.2, 0.2, 0.2);
Core.Color result = baseColor.MultiplyGPercent(3);
Core.Color result = baseColor.MultiplyRGB(g: 3);
Assert.AreEqual(new Core.Color(0.2, 0.2, 0.6, 0.2), result);
}
@ -322,7 +277,7 @@ namespace RGB.NET.Core.Tests.Color
public void MultiplyBPercentTest()
{
Core.Color baseColor = new Core.Color(0.2, 0.2, 0.2, 0.2);
Core.Color result = baseColor.MultiplyBPercent(3);
Core.Color result = baseColor.MultiplyRGB(b: 3);
Assert.AreEqual(new Core.Color(0.2, 0.2, 0.2, 0.6), result);
}
@ -335,16 +290,7 @@ namespace RGB.NET.Core.Tests.Color
public void DivideRGBPercentTest()
{
Core.Color baseColor = new Core.Color(0.2, 0.6, 0.8, 1.0);
Core.Color result = baseColor.DividePercent(3, 4, 5);
Assert.AreEqual(new Core.Color(0.2, 0.2, 0.2, 0.2), result);
}
[TestMethod]
public void DivideARGBPercentTest()
{
Core.Color baseColor = new Core.Color(0.4, 0.6, 0.8, 1.0);
Core.Color result = baseColor.DividePercent(2, 3, 4, 5);
Core.Color result = baseColor.DivideRGB(3, 4, 5);
Assert.AreEqual(new Core.Color(0.2, 0.2, 0.2, 0.2), result);
}
@ -353,7 +299,7 @@ namespace RGB.NET.Core.Tests.Color
public void DivideAPercentTest()
{
Core.Color baseColor = new Core.Color(0.6, 0.2, 0.2, 0.2);
Core.Color result = baseColor.DivideAPercent(3);
Core.Color result = baseColor.DivideA(3);
Assert.AreEqual(new Core.Color(0.2, 0.2, 0.2, 0.2), result);
}
@ -362,7 +308,7 @@ namespace RGB.NET.Core.Tests.Color
public void DivideRPercentTest()
{
Core.Color baseColor = new Core.Color(0.2, 0.6, 0.2, 0.2);
Core.Color result = baseColor.DivideRPercent(3);
Core.Color result = baseColor.DivideRGB(r: 3);
Assert.AreEqual(new Core.Color(0.2, 0.2, 0.2, 0.2), result);
}
@ -371,7 +317,7 @@ namespace RGB.NET.Core.Tests.Color
public void DivideGPercentTest()
{
Core.Color baseColor = new Core.Color(0.2, 0.2, 0.6, 0.2);
Core.Color result = baseColor.DivideGPercent(3);
Core.Color result = baseColor.DivideRGB(g: 3);
Assert.AreEqual(new Core.Color(0.2, 0.2, 0.2, 0.2), result);
}
@ -380,7 +326,7 @@ namespace RGB.NET.Core.Tests.Color
public void DivideBPercentTest()
{
Core.Color baseColor = new Core.Color(0.2, 0.2, 0.2, 0.6);
Core.Color result = baseColor.DivideBPercent(3);
Core.Color result = baseColor.DivideRGB(b: 3);
Assert.AreEqual(new Core.Color(0.2, 0.2, 0.2, 0.2), result);
}
@ -393,38 +339,20 @@ namespace RGB.NET.Core.Tests.Color
public void SetRGBTest()
{
Core.Color baseColor = new Core.Color(128, 128, 128, 128);
Core.Color result = baseColor.SetR(11).SetG(12).SetB(13);
Core.Color result = baseColor.SetRGB(11, 12, 13);
Assert.AreEqual(new Core.Color(128, 11, 12, 13), result);
}
[TestMethod]
public void SetARGBTest()
{
Core.Color baseColor = new Core.Color(128, 128, 128, 128);
Core.Color result = baseColor.SetA(10).SetR(11).SetG(12).SetB(13);
Assert.AreEqual(new Core.Color(10, 11, 12, 13), result);
}
[TestMethod]
public void SetRGBPercentTest()
{
Core.Color baseColor = new Core.Color(0.5, 0.5, 0.5, 0.5);
Core.Color result = baseColor.SetRPercent(0.2).SetGPercent(0.3).SetBPercent(0.4);
Core.Color result = baseColor.SetRGB(0.2, 0.3, 0.4);
Assert.AreEqual(new Core.Color(0.5, 0.2, 0.3, 0.4), result);
}
[TestMethod]
public void SetARGBPercentTest()
{
Core.Color baseColor = new Core.Color(0.5, 0.5, 0.5, 0.5);
Core.Color result = baseColor.SetAPercent(0.1).SetRPercent(0.2).SetGPercent(0.3).SetBPercent(0.4);
Assert.AreEqual(new Core.Color(0.1, 0.2, 0.3, 0.4), result);
}
[TestMethod]
public void SetATest()
{
@ -438,7 +366,7 @@ namespace RGB.NET.Core.Tests.Color
public void SetAPercentTest()
{
Core.Color baseColor = new Core.Color(0.5, 0.5, 0.5, 0.5);
Core.Color result = baseColor.SetAPercent(0.1);
Core.Color result = baseColor.SetA(0.1);
Assert.AreEqual(new Core.Color(0.1, 0.5, 0.5, 0.5), result);
}
@ -447,7 +375,7 @@ namespace RGB.NET.Core.Tests.Color
public void SetRTest()
{
Core.Color baseColor = new Core.Color(128, 128, 128, 128);
Core.Color result = baseColor.SetR(10);
Core.Color result = baseColor.SetRGB(r: 10);
Assert.AreEqual(new Core.Color(128, 10, 128, 128), result);
}
@ -456,7 +384,7 @@ namespace RGB.NET.Core.Tests.Color
public void SetRPercentTest()
{
Core.Color baseColor = new Core.Color(0.5, 0.5, 0.5, 0.5);
Core.Color result = baseColor.SetRPercent(0.1);
Core.Color result = baseColor.SetRGB(r: 0.1);
Assert.AreEqual(new Core.Color(0.5, 0.1, 0.5, 0.5), result);
}
@ -465,7 +393,7 @@ namespace RGB.NET.Core.Tests.Color
public void SetGTest()
{
Core.Color baseColor = new Core.Color(128, 128, 128, 128);
Core.Color result = baseColor.SetG(10);
Core.Color result = baseColor.SetRGB(g: 10);
Assert.AreEqual(new Core.Color(128, 128, 10, 128), result);
}
@ -474,7 +402,7 @@ namespace RGB.NET.Core.Tests.Color
public void SetGPercentTest()
{
Core.Color baseColor = new Core.Color(0.5, 0.5, 0.5, 0.5);
Core.Color result = baseColor.SetGPercent(0.1);
Core.Color result = baseColor.SetRGB(g: 0.1);
Assert.AreEqual(new Core.Color(0.5, 0.5, 0.1, 0.5), result);
}
@ -483,7 +411,7 @@ namespace RGB.NET.Core.Tests.Color
public void SetBTest()
{
Core.Color baseColor = new Core.Color(128, 128, 128, 128);
Core.Color result = baseColor.SetB(10);
Core.Color result = baseColor.SetRGB(b: 10);
Assert.AreEqual(new Core.Color(128, 128, 128, 10), result);
}
@ -492,7 +420,7 @@ namespace RGB.NET.Core.Tests.Color
public void SetBPercentTest()
{
Core.Color baseColor = new Core.Color(0.5, 0.5, 0.5, 0.5);
Core.Color result = baseColor.SetBPercent(0.1);
Core.Color result = baseColor.SetRGB(b: 0.1);
Assert.AreEqual(new Core.Color(0.5, 0.5, 0.5, 0.1), result);
}