Ejemplo n.º 1
0
        private void Initialize(int width, int height)
        {
            _width  = width;
            _height = height;

            _bmp = new Bitmap(_width, _height, PixelFormat.Format24bppRgb);

            BitmapData bmpData = _bmp.LockBits(new Rectangle(0, 0, _width, _height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);

            _bOffset   = bmpData.Stride - (_width * 3);
            _bWidth    = _width * 3;
            _byteCount = bmpData.Stride * _height;

            _buffer = new byte[_byteCount];

            _bmp.UnlockBits(bmpData);

            _nullXArray     = new int[_height];
            _nullColorArray = new ColorHLS[_height];
            for (int i = 0; i < _height; i++)
            {
                _nullXArray[i]     = -1;
                _nullColorArray[i] = new ColorHLS(0, 0, 0, 0);
            }
        }
Ejemplo n.º 2
0
        public void SetPixel(int x, int y, ColorHLS color, out PixelInfo oldPixelInfo)
        {
            ColorHLS oldColor;

            SetPixel(x, y, color, out oldColor);
            oldPixelInfo = new PixelInfo(x, y, oldColor);
        }
Ejemplo n.º 3
0
 public unsafe void ClearBuffer(ColorHLS color)
 {
     fixed(byte *buffer = _buffer)
     {
         ClearBuffer(buffer, color);
     }
 }
Ejemplo n.º 4
0
        public unsafe void SetPixel(int x, int y, ColorHLS color, out ColorHLS oldColor)
        {
            fixed(byte *buff = _buffer)
            {
                byte *p = buff;

                for (int i = 0; i < y; ++i)
                {
                    p += _bWidth;
                    p += _bOffset;
                }
                p += (x * 3);

                oldColor = new ColorHLS(255, 0, 0, 0);

                oldColor.Blue = (byte)p[0];
                p[0]          = color.Blue;
                p++;

                oldColor.Green = (byte)p[0];
                p[0]           = color.Green;
                p++;

                oldColor.Red = (byte)p[0];
                p[0]         = color.Red;
            }
        }
Ejemplo n.º 5
0
        public void DrawLine(int x1, int y1, int x2, int y2, ColorHLS color1, ColorHLS color2)
        {
            bool     created = false;
            ColorHLS color   = new ColorHLS(color1);

            float[,] steps = new float[0, 0];
            ColorHLS[] colors = new ColorHLS[0];

            PixelFillingDelegate pfd = delegate(int x, int y, int count, int total, bool setPixel)
            {
                if (!created)
                {
                    steps   = ColorUtils.GetGradientColorSteps(color1, color2, total);
                    created = true;
                }

                color.SetRGB(
                    (byte)steps[count, 0],
                    (byte)steps[count, 1],
                    (byte)steps[count, 2]
                    );

                if (setPixel)
                {
                    SetPixel(x, y, color);
                }

                return(color);
            };

            DrawLine(x1, y1, x2, y2, pfd);
        }
Ejemplo n.º 6
0
        private unsafe void DrawHorizontalLine(int x, int y, int width, PixelFillingDelegate pixFillingDelegate)
        {
            if (width < 0)
            {
                // width è negativa
                x += width + 1;
            }

            width = Math.Abs(width);

            fixed(byte *buff = _buffer)
            {
                byte *p = buff;

                for (int i = 0; i < y; ++i)
                {
                    p += _bWidth;
                    p += _bOffset;
                }
                p += (x * 3);

                for (int i = 0; i < width; i++)
                {
                    ColorHLS color = pixFillingDelegate(x + i, y, i, width, false);
                    p[0] = color.Blue;
                    p++;
                    p[0] = color.Green;
                    p++;
                    p[0] = color.Red;
                    p++;
                }
            }
        }
Ejemplo n.º 7
0
 public ColorHLS(ColorHLS hls)
 {
     _alpha      = hls.Alpha;
     _red        = hls.Red;
     _blue       = hls.Blue;
     _green      = hls.Green;
     _luminance  = hls.Luminance;
     _hue        = hls.Hue;
     _saturation = hls.Saturation;
 }
Ejemplo n.º 8
0
        public static ColorHLS[] CreateGradientColorArray(ColorHLS[] colors, int stepCount)
        {
            if (stepCount == 0 || colors.Length == 0)
            {
                return(new ColorHLS[0]);
            }
            if (colors.Length == 1)
            {
                return(colors);
            }
            if (stepCount == 1)
            {
                return(new ColorHLS[] { new ColorHLS(ColorUtils.BlendColors(colors[0], colors[colors.Length - 1], 50)) });
            }

            ColorHLS[] retColors = new ColorHLS[stepCount];


            float step        = stepCount / (float)(colors.Length - 1);
            int   currentStep = 0;

            for (int i = 0; i < colors.Length - 1; i++)
            {
                float r = colors[i].Red;
                float g = colors[i].Green;
                float b = colors[i].Blue;

                ColorHLS c1    = colors[i];
                ColorHLS c2    = colors[i + 1];
                float    stepR = (c2.Red - c1.Red) / step;
                float    stepG = (c2.Green - c1.Green) / step;
                float    stepB = (c2.Blue - c1.Blue) / step;

                int count = (int)(step * (i + 1));
                int k     = (int)(step * i);
                while (k < count)
                {
                    retColors[currentStep] = new ColorHLS(255, (byte)r, (byte)g, (byte)b);
                    r += stepR;
                    g += stepG;
                    b += stepB;
                    currentStep++;
                    k++;
                }
            }
            retColors[stepCount - 1] = colors[colors.Length - 1].Clone();

            return(retColors);
        }
Ejemplo n.º 9
0
        public void DrawLine(int x1, int y1, int x2, int y2, ColorHLS color)
        {
            ColorHLS             pixCol = color.Clone();
            PixelFillingDelegate pfd    = delegate(int x, int y, int count, int total, bool setPixel)
            {
                if (setPixel)
                {
                    SetPixel(x, y, pixCol);
                }

                return(pixCol);
            };

            DrawLine(x1, y1, x2, y2, pfd);
        }
Ejemplo n.º 10
0
 private unsafe void ClearBuffer(byte *buffer, ColorHLS color)
 {
     for (int y = 0; y < _height; ++y)
     {
         for (int x = 0; x < _bWidth; x += 3)
         {
             buffer[0] = color.Blue;
             ++buffer;
             buffer[0] = color.Green;
             ++buffer;
             buffer[0] = color.Red;
             ++buffer;
         }
         buffer += _bOffset;
     }
 }
Ejemplo n.º 11
0
        public static float[] GetGradientColorStep(ColorHLS startColor, ColorHLS endColor, int stepCount)
        {
            if (stepCount == 0)
            {
                return(new float[0]);
            }
            if (stepCount == 1)
            {
                return(new float[] { 0 });
            }

            float stepR = (endColor.Red - startColor.Red) / (float)stepCount;
            float stepG = (endColor.Green - startColor.Green) / (float)stepCount;
            float stepB = (endColor.Blue - startColor.Blue) / (float)stepCount;

            return(new float[] { stepR, stepG, stepB });
        }
Ejemplo n.º 12
0
        public static float[,] GetGradientColorSteps(ColorHLS startColor, ColorHLS endColor, int stepCount)
        {
            if (stepCount == 0)
            {
                return(new float[0, 0]);
            }

            float r = startColor.Red;
            float g = startColor.Green;
            float b = startColor.Blue;

            float[,] steps = new float[stepCount, 3];

            if (stepCount == 1)
            {
                for (int i = 0; i < stepCount; i++)
                {
                    steps[i, 0] = r;
                    steps[i, 1] = g;
                    steps[i, 2] = b;
                }
                return(steps);
            }

            float stepR = (endColor.Red - startColor.Red) / (float)stepCount;
            float stepG = (endColor.Green - startColor.Green) / (float)stepCount;
            float stepB = (endColor.Blue - startColor.Blue) / (float)stepCount;

            for (int i = 0; i < stepCount; i++)
            {
                steps[i, 0] = r;
                steps[i, 1] = g;
                steps[i, 2] = b;

                r += stepR;
                g += stepG;
                b += stepB;
            }

            return(steps);
        }
Ejemplo n.º 13
0
        public static ColorHLS[] CreateGradientColorArray(ColorHLS startColor, ColorHLS endColor, int stepCount)
        {
            if (stepCount == 0)
            {
                return(new ColorHLS[0]);
            }
            if (stepCount == 1)
            {
                return(new ColorHLS[] { new ColorHLS(255, (byte)((startColor.Red + endColor.Red) / 2), (byte)((startColor.Green + endColor.Green) / 2), (byte)((startColor.Blue + endColor.Blue) / 2)) });
            }

            float stepR = (endColor.Red - startColor.Red) / (float)stepCount;
            float stepG = (endColor.Green - startColor.Green) / (float)stepCount;
            float stepB = (endColor.Blue - startColor.Blue) / (float)stepCount;

            float r = startColor.Red;
            float g = startColor.Green;
            float b = startColor.Blue;

            ColorHLS[] colors = new ColorHLS[stepCount];
            for (int i = 0; i < stepCount - 1; i++)
            {
                colors[i] = new ColorHLS(
                    255,
                    (byte)Math.Round(r, MidpointRounding.ToEven),
                    (byte)Math.Round(g, MidpointRounding.ToEven),
                    (byte)Math.Round(b, MidpointRounding.ToEven)
                    );

                r += stepR;
                g += stepG;
                b += stepB;
            }
            colors[colors.Length - 1] = endColor;

            return(colors);
        }
Ejemplo n.º 14
0
        public static ColorHLS BlendColors(ColorHLS backColor, ColorHLS foreColor, float alphaPercentage)
        {
            if (alphaPercentage > 100)
            {
                throw new ArgumentOutOfRangeException("alphaPercentage must be under 100");
            }

            if (alphaPercentage == 0)
            {
                return(foreColor);
            }

            float redDiff   = foreColor.Red - backColor.Red;
            float greenDiff = foreColor.Green - backColor.Green;
            float blueDiff  = foreColor.Blue - backColor.Blue;

            alphaPercentage = alphaPercentage / 100f;

            redDiff   = (redDiff * alphaPercentage) + backColor.Red;
            greenDiff = (greenDiff * alphaPercentage) + backColor.Green;
            blueDiff  = (blueDiff * alphaPercentage) + backColor.Blue;

            return(new ColorHLS(255, (byte)redDiff, (byte)greenDiff, (byte)blueDiff));
        }
Ejemplo n.º 15
0
        public unsafe void GetPixel(int x, int y, out ColorHLS color)
        {
            fixed(byte *buff = _buffer)
            {
                byte *p = buff;

                for (int i = 0; i < y; ++i)
                {
                    p += _bWidth;
                    p += _bOffset;
                }
                p += (x * 3);

                byte b = (byte)(*p);

                p++;
                byte g = (byte)(*p);

                p++;
                byte r = (byte)(*p);

                color = new ColorHLS(255, r, g, b);
            }
        }
Ejemplo n.º 16
0
        private unsafe void DrawVerticalLine(int x, int y, int height, PixelFillingDelegate pixFillingDelegate)
        {
            if (height < 0)
            {
                // height è negativa
                y += height + 1;
            }

            height = Math.Abs(height);

            fixed(byte *buff = _buffer)
            {
                byte *p = buff;

                for (int i = 0; i < y; ++i)
                {
                    p += _bWidth;
                    p += _bOffset;
                }
                p += (x * 3);

                for (int i = 0; i < height; i++)
                {
                    ColorHLS color = pixFillingDelegate(x, y + i, i, height, false);
                    p[0] = color.Blue;
                    p++;
                    p[0] = color.Green;
                    p++;
                    p[0] = color.Red;
                    p++;

                    p += (_bWidth - 3);
                    p += _bOffset;
                }
            }
        }
Ejemplo n.º 17
0
 public PixelInfo(int x, int y, ColorHLS color)
 {
     X     = x;
     Y     = y;
     Color = color;
 }
Ejemplo n.º 18
0
        public ColorHLS Clone()
        {
            ColorHLS c = new ColorHLS(this);

            return(c);
        }
Ejemplo n.º 19
0
 public void SetPixel(PixelInfo pixelInfo, out ColorHLS oldColor)
 {
     SetPixel(pixelInfo.X, pixelInfo.Y, pixelInfo.Color, out oldColor);
 }
Ejemplo n.º 20
0
        public void SetPixel(int x, int y, ColorHLS color)
        {
            ColorHLS oldColor;

            SetPixel(x, y, color, out oldColor);
        }
Ejemplo n.º 21
0
 public PixelBuffer(int width, int height, ColorHLS backColor)
     : this(width, height)
 {
     ClearBuffer(backColor);
 }