Example #1
0
        public void GetRFromRRGGBBTest()
        {
            // Init
            string hexColor = "#128Fa5";

            // Act
            byte result = HexColorUtil.GetR(hexColor);

            // Assert
            Assert.AreEqual(18, result);
        }
Example #2
0
        public void GetRFromRGBTest()
        {
            // Init
            string hexColor = "#465";

            // Act
            byte result = HexColorUtil.GetR(hexColor);

            // Assert
            Assert.AreEqual(64, result);
        }
Example #3
0
        /// <inheritdoc />
        public System.IO.Stream ColorizePixel(System.IO.Stream imageStream, GetPixelColor getPixelColor)
        {
            #region validation

            if (imageStream == null)
            {
                throw new ArgumentNullException(nameof(imageStream));
            }

            if (getPixelColor == null)
            {
                throw new ArgumentNullException(nameof(getPixelColor));
            }

            #endregion

            // create map for colorcode-representation
            // stores already translated HEX-colors
            Dictionary <string, Rgba32> colorMap = new Dictionary <string, Rgba32>();

            using (Image <Rgba32> image = LoadImageFromStream <Rgba32>(imageStream, out IImageFormat imageFormat))
            {
                // iterate over all pixels of image
                for (int x = 0; x < image.Width; x++)
                {
                    for (int y = 0; y < image.Height; y++)
                    {
                        // get color for pixel
                        // if null, background color should be used
                        string pixelColor = getPixelColor(x, y);
                        if (!string.IsNullOrEmpty(pixelColor))
                        {
                            if (!colorMap.ContainsKey(pixelColor))
                            {
                                colorMap.Add(pixelColor, new Rgba32(HexColorUtil.GetR(pixelColor), HexColorUtil.GetG(pixelColor), HexColorUtil.GetB(pixelColor), HexColorUtil.GetA(pixelColor)));
                            }

                            image[x, y] = colorMap[pixelColor];
                        }
                    }
                }

                return(SaveAsStream(image, imageFormat));
            }
        }