public override ColorBgra Apply(ColorBgra color)
            {
                //adjust saturation
                byte intensity = color.GetIntensityByte();

                color.R = Utility.ClampToByte((intensity * 1024 + (color.R - intensity) * satFactor) >> 10);
                color.G = Utility.ClampToByte((intensity * 1024 + (color.G - intensity) * satFactor) >> 10);
                color.B = Utility.ClampToByte((intensity * 1024 + (color.B - intensity) * satFactor) >> 10);

                HsvColor hsvColor = HsvColor.FromColor(color.ToColor());
                int      hue      = hsvColor.Hue;

                hue += hueDelta;

                while (hue < 0)
                {
                    hue += 360;
                }

                while (hue > 360)
                {
                    hue -= 360;
                }

                hsvColor.Hue = hue;

                ColorBgra newColor = ColorBgra.FromColor(hsvColor.ToColor());

                newColor   = blendOp.Apply(newColor);
                newColor.A = color.A;

                return(newColor);
            }
Esempio n. 2
0
        protected override ColorBgra Render(int x, int y, ColorBgra initial, Surface source)
        {
            var shade = initial.ToColor().GetBrightness();
            if (Math.Abs(shade - BgShade) > ShadeThreshold) return initial;

            var norm = Normalize(initial);
            if (Math.Abs(BgNormal.R - norm.R) > ColorThreshold
                || Math.Abs(BgNormal.G - norm.G) > ColorThreshold
                || Math.Abs(BgNormal.B - norm.B) > ColorThreshold) {
                return initial;
            }

            var alpha = (byte)(Math.Min(255, Math.Max(0,
                Math.Max(Math.Max(
                    Math.Abs(BgColor.R - initial.R),
                    Math.Abs(BgColor.G - initial.G)),
                    Math.Abs(BgColor.B - initial.B)))
                * (1d - AlphaFalloff)));
            return ColorBgra.FromBgra(initial.B, initial.G, initial.R, alpha);
        }
Esempio n. 3
0
            public override ColorBgra Apply(ColorBgra color)
            {
                //adjust saturation
                byte intensity = color.GetIntensityByte();
                color.R = Utility.ClampToByte((intensity * 1024 + (color.R - intensity) * satFactor) >> 10);
                color.G = Utility.ClampToByte((intensity * 1024 + (color.G - intensity) * satFactor) >> 10);
                color.B = Utility.ClampToByte((intensity * 1024 + (color.B - intensity) * satFactor) >> 10);

                HsvColor hsvColor = HsvColor.FromColor(color.ToColor());
                int hue = hsvColor.Hue;

                hue += hueDelta;

                while (hue < 0)
                {
                    hue += 360;
                }

                while (hue > 360)
                {
                    hue -= 360;
                }

                hsvColor.Hue = hue;

                ColorBgra newColor = ColorBgra.FromColor(hsvColor.ToColor());
                newColor = blendOp.Apply(newColor);
                newColor.A = color.A;

                return newColor;
            }
Esempio n. 4
0
        private void RenderChannel(Graphics g, ColorBgra color, int channel, long max, float mean)
        {
            Rectangle innerRect = ClientRectangle;

            int l        = innerRect.Left;
            int t        = innerRect.Top;
            int b        = innerRect.Bottom;
            int r        = innerRect.Right;
            int channels = histogram.Channels;
            int entries  = histogram.Entries;

            long[] hist = Histogram.HistogramValues[channel];

            ++max;

            if (FlipHorizontal)
            {
                Utility.Swap(ref l, ref r);
            }

            if (!FlipVertical)
            {
                Utility.Swap(ref t, ref b);
            }

            PointF[] points = new PointF[entries + 2];

            points[entries]     = new PointF(Utility.Lerp(l, r, -1), Utility.Lerp(t, b, 20));
            points[entries + 1] = new PointF(Utility.Lerp(l, r, -1), Utility.Lerp(b, t, 20));

            for (int i = 0; i < entries; i += entries - 1)
            {
                points[i] = new PointF(
                    Utility.Lerp(l, r, (float)hist[i] / (float)max),
                    Utility.Lerp(t, b, (float)i / (float)entries));
            }

            long sum3 = hist[0] + hist[1];

            for (int i = 1; i < entries - 1; ++i)
            {
                sum3 += hist[i + 1];

                points[i] = new PointF(
                    Utility.Lerp(l, r, (float)(sum3) / (float)(max * 3.1f)),
                    Utility.Lerp(t, b, (float)i / (float)entries));

                sum3 -= hist[i - 1];
            }

            byte      intensity  = selected[channel] ? (byte)96 : (byte)32;
            ColorBgra colorPen   = ColorBgra.Blend(ColorBgra.Black, color, intensity);
            ColorBgra colorBrush = color;

            colorBrush.A = intensity;

            Pen        pen   = new Pen(colorPen.ToColor(), 1.3f);
            SolidBrush brush = new SolidBrush(colorBrush.ToColor());

            g.FillPolygon(brush, points, FillMode.Alternate);
            g.DrawPolygon(pen, points);
        }
Esempio n. 5
0
        private void DrawToGraphics(Graphics g)
        {
            ColorBgra colorSolid = ColorBgra.FromColor(this.ForeColor);
            ColorBgra colorGuide = ColorBgra.FromColor(this.ForeColor);
            ColorBgra colorGrid  = ColorBgra.FromColor(this.ForeColor);

            colorGrid.A  = 128;
            colorGuide.A = 96;

            Pen penSolid = new Pen(colorSolid.ToColor(), 1);
            Pen penGrid  = new Pen(colorGrid.ToColor(), 1);
            Pen penGuide = new Pen(colorGuide.ToColor(), 1);

            penGrid.DashStyle = DashStyle.Dash;

            g.Clear(this.BackColor);
            g.SmoothingMode = SmoothingMode.AntiAlias;

            Rectangle ourRect = ClientRectangle;

            ourRect.Inflate(-1, -1);

            if (lastMouseXY.Y >= 0)
            {
                g.DrawLine(penGuide, 0, lastMouseXY.Y, Width, lastMouseXY.Y);
            }

            if (lastMouseXY.X >= 0)
            {
                g.DrawLine(penGuide, lastMouseXY.X, 0, lastMouseXY.X, Height);
            }

            for (float f = 0.25f; f <= 0.75f; f += 0.25f)
            {
                float x = Utility.Lerp(ourRect.Left, ourRect.Right, f);
                float y = Utility.Lerp(ourRect.Top, ourRect.Bottom, f);

                g.DrawLine(penGrid,
                           Point.Round(new PointF(x, ourRect.Top)),
                           Point.Round(new PointF(x, ourRect.Bottom)));

                g.DrawLine(penGrid,
                           Point.Round(new PointF(ourRect.Left, y)),
                           Point.Round(new PointF(ourRect.Right, y)));
            }

            g.DrawLine(penGrid, ourRect.Left, ourRect.Bottom, ourRect.Right, ourRect.Top);

            float width  = this.ClientRectangle.Width;
            float height = this.ClientRectangle.Height;

            for (int c = 0; c < channels; ++c)
            {
                SortedList <int, int> channelControlPoints = controlPoints[c];
                int points = channelControlPoints.Count;

                ColorBgra color         = GetVisualColor(c);
                ColorBgra colorSelected = ColorBgra.Blend(color, ColorBgra.White, 128);

                const float penWidthNonSelected = 1;
                const float penWidthSelected    = 2;
                float       penWidth            = mask[c] ? penWidthSelected : penWidthNonSelected;
                Pen         penSelected         = new Pen(color.ToColor(), penWidth);

                color.A = 128;

                Pen        pen           = new Pen(color.ToColor(), penWidth);
                Brush      brush         = new SolidBrush(color.ToColor());
                SolidBrush brushSelected = new SolidBrush(Color.White);

                SplineInterpolator interpolator = new SplineInterpolator();
                IList <int>        xa           = channelControlPoints.Keys;
                IList <int>        ya           = channelControlPoints.Values;
                PointF[]           line         = new PointF[Entries];

                for (int i = 0; i < points; ++i)
                {
                    interpolator.Add(xa[i], ya[i]);
                }

                for (int i = 0; i < line.Length; ++i)
                {
                    line[i].X = (float)i * (width - 1) / (entries - 1);
                    line[i].Y = (float)(Utility.Clamp(entries - 1 - interpolator.Interpolate(i), 0, entries - 1)) *
                                (height - 1) / (entries - 1);
                }

                pen.LineJoin = LineJoin.Round;
                g.DrawLines(pen, line);

                for (int i = 0; i < points; ++i)
                {
                    int   k = channelControlPoints.Keys[i];
                    float x = k * (width - 1) / (entries - 1);
                    float y = (entries - 1 - channelControlPoints.Values[i]) * (height - 1) / (entries - 1);

                    const float radiusSelected    = 4;
                    const float radiusNotSelected = 3;
                    const float radiusUnMasked    = 2;

                    bool       selected = (mask[c] && pointsNearMousePerChannel[c] == i);
                    float      size     = selected ? radiusSelected : (mask[c] ? radiusNotSelected : radiusUnMasked);
                    RectangleF rect     = Utility.RectangleFromCenter(new PointF(x, y), size);

                    g.FillEllipse(selected ? brushSelected : brush, rect.X, rect.Y, rect.Width, rect.Height);
                    g.DrawEllipse(selected ? penSelected : pen, rect.X, rect.Y, rect.Width, rect.Height);
                }

                pen.Dispose();
            }

            penSolid.Dispose();
            penGrid.Dispose();
            penGuide.Dispose();
        }
Esempio n. 6
0
        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.CompositingMode = CompositingMode.SourceOver;
            int scaledSwatchSize = UI.ScaleWidth(this.unscaledSwatchSize);
            int swatchColumns    = this.ClientSize.Width / scaledSwatchSize;

            Point mousePt = Control.MousePosition;

            mousePt = PointToClient(mousePt);
            int activeIndex = MouseXYToColorIndex(mousePt.X, mousePt.Y);

            for (int i = 0; i < this.colors.Count; ++i)
            {
                ColorBgra c = this.colors[i];

                int swatchX = i % swatchColumns;
                int swatchY = i / swatchColumns;

                Rectangle swatchRect = new Rectangle(
                    swatchX * scaledSwatchSize,
                    swatchY * scaledSwatchSize,
                    scaledSwatchSize,
                    scaledSwatchSize);

                UI.ButtonState state;

                if (this.mouseDown)
                {
                    if (i == this.mouseDownIndex)
                    {
                        state = UI.ButtonState.Pressed;
                    }
                    else
                    {
                        state = UI.ButtonState.Normal;
                    }
                }
                else if (i == activeIndex)
                {
                    state = UI.ButtonState.Hot;
                }
                else
                {
                    state = UI.ButtonState.Normal;
                }

                bool drawOutline;

                switch (state)
                {
                case UI.ButtonState.Hot:
                    drawOutline = true;
                    break;

                case UI.ButtonState.Pressed:
                    drawOutline = false;
                    break;

                case UI.ButtonState.Disabled:
                case UI.ButtonState.Normal:
                    drawOutline = false;
                    break;

                default:
                    throw new InvalidEnumArgumentException();
                }

                Utility.DrawColorRectangle(e.Graphics, swatchRect, c.ToColor(), drawOutline);
            }

            if (this.blinkHighlight)
            {
                int   period = (Math.Abs(Environment.TickCount) / blinkInterval) % 2;
                Color color;

                switch (period)
                {
                case 0:
                    color = SystemColors.Window;
                    break;

                case 1:
                    color = SystemColors.Highlight;
                    break;

                default:
                    throw new InvalidOperationException();
                }

                using (Pen pen = new Pen(color))
                {
                    e.Graphics.DrawRectangle(pen, new Rectangle(0, 0, Width - 1, Height - 1));
                }
            }

            base.OnPaint(e);
        }
Esempio n. 7
0
        private void RenderColorAddIcon(ColorBgra newColor)
        {
            if (this.colorAddIcon == null)
            {
                this.colorAddIcon = new Bitmap(16, 16, PixelFormat.Format32bppArgb);
            }

            using (Graphics g = Graphics.FromImage(this.colorAddIcon))
            {
                Rectangle rect = new Rectangle(0, 0, this.colorAddIcon.Width - 2, this.colorAddIcon.Height - 2);
                Utility.DrawColorRectangle(g, rect, newColor.ToColor(), true);
                g.DrawImage(this.colorAddOverlay, 0, 0);
            }

            this.colorAddButton.Image = this.colorAddIcon;
            this.colorAddButton.Invalidate();
        }
Esempio n. 8
0
        /// <summary>
        /// Whenever a color is changed via RGB methods, call this and the HSV
        /// counterparts will be sync'd up.
        /// </summary>
        /// <param name="newColor">The RGB color that should be converted to HSV.</param>
        private void SyncHsvFromRgb(ColorBgra newColor)
        {
            if (ignore == 0) 
            {
                ignore++;
                HsvColor hsvColor = HsvColor.FromColor(newColor.ToColor());

                Utility.SetNumericUpDownValue(hueUpDown, hsvColor.Hue);
                Utility.SetNumericUpDownValue(saturationUpDown, hsvColor.Saturation);
                Utility.SetNumericUpDownValue(valueUpDown, hsvColor.Value);

                SetColorGradientValuesHsv(hsvColor.Hue, hsvColor.Saturation, hsvColor.Value);
                SetColorGradientMinMaxColorsHsv(hsvColor.Hue, hsvColor.Saturation, hsvColor.Value);

                colorWheel.HsvColor = hsvColor;
                ignore--;
            }
        }