/// <summary> /// Tries to increase a component of the 16-bit color by the given value, /// starting from the green component with the highest value range. /// </summary> /// <param name="color">The 16-bit color to increase.</param> /// <param name="value">The value of how much to attempt to increase a component.</param> /// <returns>True if any of the components could be increased by the given value, /// false otherwise.</returns> private static bool TryIncrease(ref Color565 color, int value) { const int maxValue6Bit = 63; const int maxValue5Bit = 31; if (color.G + value > 0 && color.G + value <= maxValue6Bit) { color = Color565.FromRgb(color.R, color.G + value, color.B); return(true); } if (color.R + value > 0 && color.R + value <= maxValue5Bit) { color = Color565.FromRgb(color.R + value, color.G, color.B); return(true); } if (color.B + value > 0 && color.B + value <= maxValue5Bit) { color = Color565.FromRgb(color.R, color.G, color.B + value); return(true); } return(false); }
public void ConstructFromBytes() { var color = Color565.FromRgb((byte)10, (byte)51, (byte)23); Assert.AreEqual(10, color.R); Assert.AreEqual(51, color.G); Assert.AreEqual(23, color.B); }
/// <summary> /// Converts the 32-bit color into a 16-bit R5:G6:B5 color by shifting the bits right for /// each color component. /// </summary> public static Color565 To16Bit(Color color) { int r = color.R >> 3; int g = color.G >> 2; int b = color.B >> 3; return(Color565.FromRgb(r, g, b)); }
/// <summary> /// Blends the two colors by taking the average of each color component. /// </summary> public static Color565 Blend(Color565 a, Color565 b) { var c = BlendComponents( new[] { a.R, a.G, a.B }, new[] { b.R, b.G, b.B }); return(Color565.FromRgb(c[0], c[1], c[2])); }
public void ConstructFrom32BitIntegers() { var color = Color565.FromRgb(10, 51, 23); Assert.AreEqual(10, color.R); Assert.AreEqual(51, color.G); Assert.AreEqual(23, color.B); }
/// <summary> /// Linearly interpolates to two thirds of the distance from the /// first color to the second color for each color component. /// </summary> public static Color565 LerpTwoThirds(Color565 a, Color565 b) { var c = LerpComponents( new[] { a.R, a.G, a.B }, new[] { b.R, b.G, b.B }); return(Color565.FromRgb(c[0], c[1], c[2])); }
public void Blend16BitColors() { var a = Color565.FromRgb(30, 60, 20); var b = Color565.FromRgb(20, 60, 25); var expected = Color565.FromRgb(25, 60, 23); var result = ColorUtility.Blend(a, b); Assert.AreEqual(expected, result); }
public void Lerp16BitColors() { var a = Color565.FromRgb(10, 60, 20); var b = Color565.FromRgb(30, 60, 15); var expected = Color565.FromRgb(23, 60, 17); var result = ColorUtility.LerpTwoThirds(a, b); Assert.AreEqual(expected, result); }
public void DifferentiationDecreasesMinColorByOneWhenMaxCannotBeIncreased() { var minOriginal = Color565.FromRgb(31, 63, 31); var maxOriginal = Color565.FromRgb(31, 63, 31); var min = minOriginal; var max = maxOriginal; ColorUtility.TryDifferentiateByOne(ref min, ref max); Assert.AreEqual(Color565.FromRgb(31, 62, 31), min); Assert.AreEqual(maxOriginal, max); }
public void DifferentiationIncreasesMaxColorByOneWhenMaxCanBeIncreased() { var minOriginal = Color565.FromRgb(15, 43, 9); var maxOriginal = Color565.FromRgb(15, 43, 9); var min = minOriginal; var max = maxOriginal; ColorUtility.TryDifferentiateByOne(ref min, ref max); Assert.AreEqual(minOriginal, min); Assert.AreEqual(Color565.FromRgb(15, 44, 9), max); }
public void ConstructionClampsComponentsForBytes() { var expected = Color565.FromRgb((byte)31, (byte)63, (byte)31); var color1 = Color565.FromRgb((byte)255, (byte)255, (byte)255); var color2 = Color565.FromRgb((byte)150, (byte)230, (byte)130); Assert.AreEqual(expected, color1); Assert.AreEqual(expected, color2); Assert.AreEqual(MaxValue16Bit, color1.Value); Assert.AreEqual(MaxValue16Bit, color2.Value); }
public void DifferentiationDoesNothingWhenColorsAreDifferent() { var minOriginal = Color565.FromRgb(15, 31, 21); var maxOriginal = Color565.FromRgb(13, 24, 14); var min = minOriginal; var max = maxOriginal; ColorUtility.TryDifferentiateByOne(ref min, ref max); Assert.AreEqual(minOriginal, min); Assert.AreEqual(maxOriginal, max); }
public void CalculateDistanceBetween16BitColors() { var a = Color565.FromRgb(10, 16, 9); var b = Color565.FromRgb(20, 63, 31); var expected = Math.Sqrt((10 - 20) * (10 - 20) + (16 - 63) * (16 - 63) + (9 - 31) * (9 - 31)); var result = ColorUtility.Distance(a, b); const double delta = 0.0001d; Assert.AreEqual(expected, result, delta); }
public void Convert32BitColorTo16Bit() { var color1 = Color.FromArgb(74, 207, 231); var expected1 = Color565.FromRgb(9, 51, 28); var color2 = Color.FromArgb(0, 255, 255); var expected2 = Color565.FromRgb(0, 63, 31); var result1 = ColorUtility.To16Bit(color1); var result2 = ColorUtility.To16Bit(color2); Assert.AreEqual(expected1, result1); Assert.AreEqual(expected2, result2); }
public void ConstructionClampsComponentsFor32BitIntegers() { var expected = Color565.FromRgb(31, 63, 31); var color1 = Color565.FromRgb(3455, 23145, 55132); var color2 = Color565.FromRgb(255, 255, 255); var color3 = Color565.FromRgb(150, 230, 130); Assert.AreEqual(expected, color1); Assert.AreEqual(expected, color2); Assert.AreEqual(expected, color3); Assert.AreEqual(MaxValue16Bit, color1.Value); Assert.AreEqual(MaxValue16Bit, color2.Value); Assert.AreEqual(MaxValue16Bit, color3.Value); }
public void FormatIsR5G6B5() { var color = Color565.FromRgb(10, 51, 23); // 10 = _0 1010 -> R5 // 51 = 11 0011 -> G6 // 23 = _1 0111 -> B5 // R5 G6 B5 // 0101 0|110 011|1 0111 16-bit layout const int expected = 22135; var value = color.Value; Assert.AreEqual(expected, value); }
public void CalculateClosestColor() { var target = Color565.FromRgb(15, 31, 15); var expected = Color565.FromRgb(13, 29, 14); var colors = new [] { Color565.FromRgb(0, 0, 0), Color565.FromRgb(31, 63, 31), Color565.FromRgb(9, 56, 45), expected, Color565.FromRgb(24, 45, 14), }; var closest = ColorUtility.GetClosestColor(colors, target); Assert.AreEqual(expected, closest); }