Exemplo n.º 1
0
        public static void ColorTriangleInPlace(Graphics g, int angle, int size, int thickness)
        {
            var saved = g.Save();

            g.SmoothingMode   = SmoothingMode.AntiAlias;
            g.PixelOffsetMode = PixelOffsetMode.HighQuality;
            g.TranslateTransform(thickness, thickness);
            g.TranslateTransform(size / 2, size / 2);
            g.RotateTransform(angle - 120);
            g.TranslateTransform(-size / 2, -size / 2);

            var origin = new Point(size / 2, size / 2);
            var first  = new PointF(size, size / 2.0f);
            var points = new PointF[3]
            {
                first, ColorPickUtil.RotatePoint(first, origin, ColorPickUtil.DegreeToRadian(120)),
                ColorPickUtil.RotatePoint(first, origin, ColorPickUtil.DegreeToRadian(240))
            };

            for (var i = 0; i < points.Length; ++i)
            {
                points[i] = new PointF(Snap(points[i].X, 0.5f), Snap(points[i].Y, 0.5f));
            }

            using (var pgb = new PathGradientBrush(points, WrapMode.TileFlipXY))
            {
                pgb.SurroundColors = new[] { Color.Black, Color.White };
                pgb.CenterPoint    = ColorPickUtil.RotatePoint(first, origin, ColorPickUtil.DegreeToRadian(120));
                pgb.CenterColor    = ColorManager.HSVtoRGB((short)angle, 100, 100, 255);
                g.FillPolygon(pgb, points);
            }
            g.Restore(saved);
        }
Exemplo n.º 2
0
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = e.Graphics;

            ColorPickRenderer.ColorTriangleInPlace(g, _currentHue, Width - thickness * 2, thickness);

            g.DrawImageUnscaled(wheel, 0, 0);

            g.SmoothingMode = SmoothingMode.AntiAlias;

            PointF p = ColorPickUtil.RotatePoint(clickPoint, new PointF(Width / 2.0f, Width / 2.0f),
                                                 ColorPickUtil.DegreeToRadian(rotatePoint ? _currentHue - initRot : 0));

            if (drawPoint)
            {
                g.DrawEllipse(new Pen(Negative(CurrentHSV), 2), new RectangleF(p.X - 3, p.Y - 3, 6, 6));
            }

            PointF start = ColorPickUtil.RotatePoint(new PointF(Width - thickness, (Height / 2.0f) - 1.5f),
                                                     new PointF(Width / 2.0f, Width / 2.0f),
                                                     ColorPickUtil.DegreeToRadian(_currentHue));
            PointF end = ColorPickUtil.RotatePoint(new PointF(Width, (Height / 2.0f) - 1.5f),
                                                   new PointF(Width / 2.0f, Width / 2.0f),
                                                   ColorPickUtil.DegreeToRadian(_currentHue));

            g.DrawLine(new Pen(Negative(new ColorManager.HSVColor(CurrentHSV.H, 100, 100)), 2), start, end);
        }
Exemplo n.º 3
0
        public PointF ClipPoint(PointF p, int angle)
        {
            /*if (InTriangle(p, Triangle(angle)))
             *      return p;
             * else
             * {
             *      PointF _clickPosition = ColorPickUtil.RotatePoint(p, new PointF(Width / 2.0f, Width / 2.0f), ColorPickUtil.DegreeToRadian(-angle));
             *      PointF[] polygon = Triangle(0);
             *      float width = (polygon[0].X - polygon[1].X);
             *      float height = (polygon[1].Y - polygon[2].Y);
             *
             *      PointF offsetFromPoint = new PointF(polygon[0].X - _clickPosition.X, polygon[0].Y - _clickPosition.Y);
             *
             *      float maxY = (((polygon[0].X - _clickPosition.X) / (float)width) * (float)height);
             *      float highestY = (((polygon[0].X - polygon[1].X) / (float)width) * (float)height);
             *
             *      PointF clippedPosition = _clickPosition;
             *
             *      if (offsetFromPoint.X > width)
             *      {
             *              clippedPosition.X = polygon[1].X;
             *              maxY = highestY;
             *      }
             *
             *      if (offsetFromPoint.Y > maxY - (maxY / 2))
             *              clippedPosition.Y = (polygon[0].Y - (maxY - (maxY / 2)));
             *      else if (offsetFromPoint.Y < -(maxY - (maxY / 2)))
             *              clippedPosition.Y = (polygon[0].Y + (maxY - (maxY / 2)));
             *
             *      if (clippedPosition.X > polygon[0].X)
             *              clippedPosition = polygon[0];
             *
             *      return ColorPickUtil.RotatePoint(clippedPosition, new PointF(Width / 2.0f, Width / 2.0f), ColorPickUtil.DegreeToRadian(angle));
             * }*/

            PointF _clickPosition = ColorPickUtil.RotatePoint(p, new PointF(Width / 2.0f, Width / 2.0f),
                                                              ColorPickUtil.DegreeToRadian(-angle));

            PointF[] polygon = Triangle(0);

            if (_clickPosition.X < polygon[2].X)
            {
                _clickPosition.X = polygon[2].X;
            }
            if (_clickPosition.Y < polygon[2].Y)
            {
                _clickPosition.Y = polygon[2].Y;
            }

            return(ColorPickUtil.RotatePoint(_clickPosition, new PointF(Width / 2.0f, Width / 2.0f),
                                             ColorPickUtil.DegreeToRadian(angle)));
        }
Exemplo n.º 4
0
        public PointF[] Triangle(int angle)
        {
            PointF first = ColorPickUtil.RotatePoint(new PointF(Width - thickness, (Width - thickness) / 2.0f),
                                                     new PointF(Width / 2.0f, Width / 2.0f),
                                                     ColorPickUtil.DegreeToRadian(angle + 7));

            return(new PointF[3]
            {
                first,
                ColorPickUtil.RotatePoint(first, new PointF(Width / 2.0f, Width / 2.0f), ColorPickUtil.DegreeToRadian(120)),
                ColorPickUtil.RotatePoint(first, new PointF(Width / 2.0f, Width / 2.0f), ColorPickUtil.DegreeToRadian(240))
            });
        }
Exemplo n.º 5
0
        public static Bitmap ColorWheel(int size, int width, int multisampling)
        {
            if (width < 1 || size < 1)
            {
                return(null);
            }

            int m = multisampling;

            size  = size * m;
            width = width * m;

            using (var b = new Bitmap(size, size))
            {
                using (Graphics g = Graphics.FromImage(b))
                {
                    g.Clear(Color.Transparent);
                    var rect = new Rectangle(0, 0, size - m, size - m);
                    using (var wheel_path = new GraphicsPath())
                    {
                        wheel_path.AddEllipse(rect);
                        wheel_path.Flatten();

                        int num_pts         = (wheel_path.PointCount - 1);
                        var surround_colors = new Color[wheel_path.PointCount];
                        for (int i = 0; i < wheel_path.PointCount; i++)
                        {
                            surround_colors[i] = new ColorManager.HSVColor((short)((double)i / num_pts * 360), 100, 100).ToColor();
                        }

                        using (var brush = new PathGradientBrush(wheel_path))
                        {
                            brush.CenterColor    = SystemColors.Window;
                            brush.SurroundColors = surround_colors;
                            brush.FocusScales    = new PointF(100.0f, 100.0f);
                            g.FillEllipse(brush, rect);
                        }
                        using (var brush = new SolidBrush(Color.White))
                            g.FillEllipse(brush, new Rectangle(width, width, size - width * 2, size - width * 2));

                        //replace all the white with color.transparent :)
                        b.MakeTransparent(Color.White);
                    }
                }
                return(ColorPickUtil.ResizeImage(b, size / m));
            }
        }
Exemplo n.º 6
0
        public PointF LocationForColorHSV(ColorManager.HSVColor hsv)
        {
            /*PointF[] polygon = Triangle(0);
             * float x = (float)((((float)hsv.S / 100.0f) * (polygon[0].X - polygon[1].X)) + polygon[1].X);
             * //if (x > polygon[0].X)
             * //    x = polygon[0].X;
             *
             * PointF p = ColorPickUtil.RotatePoint(polygon[0], new PointF(Width / 2, Height / 2), ColorPickUtil.DegreeToRadian(30));
             *
             * float y = (float)(((float)hsv.V / 100.0f) * (Triangle(30)[1].Y + thickness));
             * //if (y > Triangle(30)[1].Y)
             * //    y = Triangle(30)[1].Y;
             *
             * float height = (polygon[1].Y - polygon[2].Y - thickness);
             * p.Y = y + (height / 2);
             *
             * p = ColorPickUtil.RotatePoint(p, new PointF(Width/2, Height/2), ColorPickUtil.DegreeToRadian(-30));
             *
             * return new PointF(x, p.Y);*/
            PointF[] InnerPoints = Triangle(0);
            double   h           = hsv.H;
            double   s           = hsv.S / 100.0;
            double   v           = hsv.V / 100.0;

            PointF vH = InnerPoints[0];
            PointF vS = InnerPoints[1];
            PointF vV = InnerPoints[2];

            // saturation first, then value
            // this design matches with the picture from wiki

            var vStoH   = new PointF((vH.X - vS.X) * (float)s, (vH.Y - vS.Y) * (float)s);
            var vS2     = new PointF(vS.X + vStoH.X, vS.Y + vStoH.Y);
            var vVtovS2 = new PointF((vS2.X - vV.X) * (float)v, (vS2.Y - vV.Y) * (float)v);
            var final   = new PointF(vV.X + vVtovS2.X, vV.Y + vVtovS2.Y);

            return(ColorPickUtil.RotatePoint(final, new PointF(Width / 2.0f, Width / 2.0f),
                                             ColorPickUtil.DegreeToRadian(_currentHue)));
        }