Daire Şeklinde Renk Paleti Oluştur - CSharp


Renk seçimi için kullanılabilecek, daire şeklinde bir renk seçme paleti oluşturan csharp sınıfı alternatifidir.

Kod


public class ColorWheel
{
    /// <summary>
    /// Draw a color wheel in the indicated area. 
    /// </summary>
    /// <param name="gr"></param>
    /// <param name="outline_color"></param>
    /// <param name="xmin"></param>
    /// <param name="ymin"></param>
    /// <param name="wid"></param>
    /// <param name="hgt"></param>
    public static void Draw(Graphics gr, Color outline_color,
        int xmin, int ymin, int wid, int hgt)
    {
        Rectangle rect = new Rectangle(xmin, ymin, wid, hgt);
        GraphicsPath wheel_path = new GraphicsPath();
        wheel_path.AddEllipse(rect);
        wheel_path.Flatten();

        float num_pts = (wheel_path.PointCount - 1) / 6;
        Color[] surround_colors = new Color[wheel_path.PointCount];

        int index = 0;
        InterpolateColors(surround_colors, ref index,
            1 * num_pts, 255, 255, 0, 0, 255, 255, 0, 255);
        InterpolateColors(surround_colors, ref index,
            2 * num_pts, 255, 255, 0, 255, 255, 0, 0, 255);
        InterpolateColors(surround_colors, ref index,
            3 * num_pts, 255, 0, 0, 255, 255, 0, 255, 255);
        InterpolateColors(surround_colors, ref index,
            4 * num_pts, 255, 0, 255, 255, 255, 0, 255, 0);
        InterpolateColors(surround_colors, ref index,
            5 * num_pts, 255, 0, 255, 0, 255, 255, 255, 0);
        InterpolateColors(surround_colors, ref index,
            wheel_path.PointCount, 255, 255, 255, 0, 255, 255, 0, 0);

        using (PathGradientBrush path_brush =
            new PathGradientBrush(wheel_path))
        {
            path_brush.CenterColor = Color.White;
            path_brush.SurroundColors = surround_colors;

            gr.FillPath(path_brush, wheel_path);

            // It looks better if we outline the wheel.
            using (Pen thick_pen = new Pen(outline_color, 2))
            {
                gr.DrawPath(thick_pen, wheel_path);
            }
        }

        //// Uncomment the following to draw the path's points.
        //for (int i = 0; i < wheel_path.PointCount; i++)
        //{
        //    gr.FillEllipse(Brushes.Black,
        //        wheel_path.PathPoints[i].X - 2,
        //        wheel_path.PathPoints[i].Y - 2,
        //        4, 4);
        //}
    }

    /// <summary>
    /// Fill in colors interpolating between the from and to values.
    /// </summary>
    /// <param name="surround_colors"></param>
    /// <param name="index"></param>
    /// <param name="stop_pt"></param>
    /// <param name="from_a"></param>
    /// <param name="from_r"></param>
    /// <param name="from_g"></param>
    /// <param name="from_b"></param>
    /// <param name="to_a"></param>
    /// <param name="to_r"></param>
    /// <param name="to_g"></param>
    /// <param name="to_b"></param>
    public static void InterpolateColors(Color[] surround_colors,
        ref int index, float stop_pt,
        int from_a, int from_r, int from_g, int from_b,
        int to_a, int to_r, int to_g, int to_b)
    {
        int num_pts = (int)stop_pt - index;
        float a = from_a, r = from_r, g = from_g, b = from_b;
        float da = (to_a - from_a) / (num_pts - 1);
        float dr = (to_r - from_r) / (num_pts - 1);
        float dg = (to_g - from_g) / (num_pts - 1);
        float db = (to_b - from_b) / (num_pts - 1);

        for (int i = 0; i < num_pts; i++)
        {
            surround_colors[index++] =
                Color.FromArgb((int)a, (int)r, (int)g, (int)b);
            a += da;
            r += dr;
            g += dg;
            b += db;
        }
    }
}

Etiketler
csharp