예제 #1
0
        /// <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);
        }
예제 #2
0
        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);
        }
예제 #3
0
        /// <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));
        }
예제 #4
0
        /// <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]));
        }
예제 #5
0
        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);
        }
예제 #6
0
        /// <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]));
        }
예제 #7
0
        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);
        }
예제 #8
0
        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);
        }
예제 #9
0
        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);
        }
예제 #10
0
        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);
        }
예제 #11
0
        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);
        }
예제 #12
0
        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);
        }
예제 #13
0
        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);
        }
예제 #14
0
        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);
        }
예제 #15
0
        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);
        }
예제 #16
0
        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);
        }
예제 #17
0
        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);
        }