本帖最后由 caozhy 于 2013-02-06 22:50:03 编辑

解决方案 »

  1.   

    传不上去  神奇的CSDN啊
      

  2.   

    代码如下
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;namespace ColorPicker
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void Form1_Load(object sender, EventArgs e)
            {
                this.label1.ForeColor = this.label2.ForeColor = Color.White;
            ///    Bitmap b = new Bitmap(typeof(Button),"Resources.colorbarIndicators.bmp");
            }        private void colorWheel1_SelectedColorChanged(object sender, EventArgs e)
            {
                this.BackColor = this.colorWheel1.SelectedHSLColor.Color;
                this.label1.Text = this.colorWheel1.SelectedHSLColor.ToString();
                this.label2.Text = this.colorWheel1.SelectedHSLColor.Color.ToString();
            }        private void button1_Click(object sender, EventArgs e)
            {
                ColorDialog cd = new ColorDialog();
                cd.ShowDialog();
                this.colorWheel1.SelectedHSLColor =new HSLColor(cd.Color);
            }        private void colorWheel2_SelectedColorChanged(object sender, EventArgs e)
            {
                this.colorWheel1.SelectedHSLColor = this.colorWheel2.SelectedHSLColor;
            }  
        }
    }
      

  3.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Data;
    using System.Text;
    using System.Windows.Forms;namespace ColorPicker
    {
    class ColorWheel : Control
    {
    public event EventHandler SelectedColorChanged;

    Color m_frameColor = Color.CadetBlue;
    HSLColor m_selectedColor = new HSLColor(Color.BlanchedAlmond);
    PathGradientBrush m_brush = null;
    List<PointF> m_path = new List<PointF>();
    List<Color> m_colors = new List<Color>();
    double m_wheelLightness = 0.5;
            RectangleF square = new RectangleF();
            PointF mousesquarepoint; public HSLColor SelectedHSLColor
    {
    get { return m_selectedColor; }
    set 
    {
    if (m_selectedColor == value)
    return;
    m_selectedColor = value;
    if (SelectedColorChanged != null)
    SelectedColorChanged(this, null);
                    Refresh();
                }
    } public ColorWheel()
    {
    SetStyle(ControlStyles.SupportsTransparentBackColor, true);
    DoubleBuffered = true;
    SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint, true);
    }

    protected override void OnPaint(PaintEventArgs e)
    {
    using (SolidBrush b = new SolidBrush(Color.Gray))
    {
    e.Graphics.FillRectangle(b, ClientRectangle);
    }
    RectangleF wheelrect = WheelRectangle;
    Util.DrawFrame(e.Graphics, wheelrect, 6, m_frameColor);

    wheelrect = ColorWheelRectangle;
    PointF center = Util.Center(wheelrect);
        e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
    if (m_brush == null)
    {
    m_brush = new PathGradientBrush(m_path.ToArray(), WrapMode.Clamp);
    m_brush.CenterPoint = center;
    m_brush.CenterColor = Color.White;
    m_brush.SurroundColors = m_colors.ToArray();
    }
    e.Graphics.FillPie(m_brush, Util.Rect(wheelrect), 0, 360);
                DrawColorSelector1(e.Graphics);
                DrawCircle(e.Graphics);
                DrawRect(e.Graphics);
                DrawColorSelector2(e.Graphics);
    } protected override void OnResize(EventArgs e)
    {
    base.OnResize(e);
    if (m_brush != null)
    m_brush.Dispose();
    m_brush = null;
    RecalcWheelPoints();
    }        protected override void OnMouseMove(MouseEventArgs e)
            {
                base.OnMouseMove(e);
                PointF mousepoint = new PointF(e.X, e.Y);
                if (e.Button == MouseButtons.Left)
                    SetColor(mousepoint);
            } protected override void OnMouseDown(MouseEventArgs e)
    {
    base.OnMouseDown(e);
                PointF mousepoint = new PointF(e.X, e.Y);
    if (e.Button == MouseButtons.Left)
    SetColor(mousepoint);
    } protected override bool ProcessDialogKey(Keys keyData)
    {
    HSLColor c = SelectedHSLColor;
    double hue = c.Hue;
    int step = 1;
    if ((keyData & Keys.Control) == Keys.Control)
    step = 5; if ((keyData & Keys.Up) == Keys.Up)
    hue += step;
    if ((keyData & Keys.Down) == Keys.Down)
    hue -= step;
    if (hue >= 360)
    hue = 0;
    if (hue < 0)
    hue = 359; if (hue != c.Hue)
    {
    c.Hue = hue;
    SelectedHSLColor = c;
    return true;
    }
    return base.ProcessDialogKey(keyData);
    }        bool InCircularRing(PointF p)
            {
                PointF center = Util.Center(ColorWheelRectangle);
                double radius = Radius(ColorWheelRectangle);
                double distance = 0;
                distance =Math.Sqrt((center.X - p.X) * (center.X - p.X) + (center.Y - p.Y) * (center.Y - p.Y));
                return (distance > radius * 0.8 && distance < radius);
            }        bool InSquare(PointF p)
            {
                return square.Contains(p);
            } RectangleF ColorSelector1Rectangle
    {
    get
    {
    HSLColor color = m_selectedColor;
    double angleR = color.Hue * Math.PI / 180;
    PointF center = Util.Center(ColorWheelRectangle);
                    double radius = Radius(ColorWheelRectangle);
                    radius *= 0.9;
    double x = center.X + Math.Cos(angleR) * radius;
    double y = center.Y - Math.Sin(angleR) * radius;
                    RectangleF colrect = new RectangleF(new PointF((float)x, (float)y), new Size(1, 1));
                    return colrect;
    }
    }        RectangleF ColorSelector2Rectangle
            {
                get
                {
                    double w = (1 - m_selectedColor.Saturation) * square.Width;
                    double h = (1 - m_selectedColor.Lightness) * square.Height;
                    return new RectangleF((float)(square.X + w), (float)(square.Y + h), 1,1);
                }
            }        void DrawColorSelector1(Graphics dc)
            {
                RectangleF r = Util.Rect(ColorSelector1Rectangle);
                PointF center = Util.Center(r);
                Image image = SelectorImages.Image(SelectorImages.eIndexes.Donut);
                dc.DrawImageUnscaled(image, (int)(center.X - image.Width / 2), (int)(center.Y - image.Height / 2));
            }        private void DrawColorSelector2(Graphics dc)
            {
                RectangleF r = Util.Rect(ColorSelector2Rectangle);
                PointF center = Util.Center(r);
                Image image = SelectorImages.Image(SelectorImages.eIndexes.Donut);
                dc.DrawImageUnscaled(image, (int)(center.X - image.Width / 2), (int)(center.Y - image.Height / 2));
            }        PointF angleToPoint(PointF center, float angle, float radius)
            {
                return new PointF(
                    center.X + radius * (float)Math.Cos(angle * Math.PI / 180),
                    center.Y + radius * (float)Math.Sin(angle * Math.PI / 180)
                    );
            }        void DrawCircle(Graphics dc)
            {
                Pen pen=new Pen(Color.Gray);
                PointF center = Util.Center(ColorWheelRectangle);
                float radius = Radius(ColorWheelRectangle)*0.8f;
                float angle=0f;
                float angleStep=0.5f;
                for(int i=0;i<360;i++)
                {
                    PointF pointf1=angleToPoint(center,angle,radius);
                    PointF pointf2=angleToPoint(center,angle+180.0f,radius);
                    dc.DrawLine(pen,pointf1,pointf2);
                    angle += angleStep;
                }
            }        void DrawRect(Graphics dc)
            {
                HSLColor color = m_selectedColor;
                PointF center = Util.Center(ColorWheelRectangle);
                float halfside = Radius(ColorWheelRectangle) * 0.8f/(float)Math.Sqrt(2);
                float step = halfside * 2 / 360;
                float x1 = center.X - halfside;
                float y1 = center.Y - halfside;
                float x2 = center.X + halfside;
                float y2 = center.Y - halfside;
                square.X = x1;
                square.Y = y1;
                square.Width = halfside * 2;
                square.Height = halfside * 2;
                for (int i = 0; i < 360; i++)
                {
                    Color c1 = new HSLColor(color.Hue, 1, (double)(360-i)/360).Color;
                    Color c2 = new HSLColor(color.Hue, 0.00001, (double)(360 - i) / 360).Color; //S不等于0
                    Pen pen=new Pen(new LinearGradientBrush(new PointF(x1,y1),new PointF(x2,y2),c1,c2));
                    dc.DrawLine(pen, x1, y1, x2, y2);
                    y1 += step;
                    y2 += step;
                }
            }
    RectangleF WheelRectangle
    {
    get 

    Rectangle r = ClientRectangle;
    r.Width -= 1;
    r.Height -= 1;
    return r; 
    }
    }
    RectangleF ColorWheelRectangle
    {
    get
    {
    RectangleF r = WheelRectangle;
    r.Inflate(-5, -5);
    return r;
    }
    }
    float Radius(RectangleF r)
    {
    PointF center = Util.Center(r);
    float radius = Math.Min((r.Width / 2), (r.Height / 2));
    return radius;
    }
    void RecalcWheelPoints()
    {
    m_path.Clear();
    m_colors.Clear(); PointF center = Util.Center(ColorWheelRectangle);
    float radius = Radius(ColorWheelRectangle);
    double angle = 0;
    double fullcircle = 360;
    double step = 5;
    while (angle < fullcircle)
    {
    double angleR = angle * (Math.PI/180);
    double x = center.X + Math.Cos(angleR) * radius;
    double y = center.Y - Math.Sin(angleR) * radius;
    m_path.Add(new PointF((float)x,(float)y));
    m_colors.Add(new HSLColor(angle, 1, m_wheelLightness).Color);
    angle += step; 
    }
    }
            void SetColor(PointF mousepoint)
            {
                HSLColor color = m_selectedColor;
                if (InCircularRing(mousepoint))
                {
                    PointF center = Util.Center(ColorWheelRectangle);
                    double dx = Math.Abs(mousepoint.X - center.X);
                    double dy = Math.Abs(mousepoint.Y - center.Y);
                    double angle = Math.Atan(dy / dx) / Math.PI * 180;
                    if (mousepoint.X < center.X)
                        angle = 180 - angle;
                    if (mousepoint.Y > center.Y)
                        angle = 360 - angle;
                    SelectedHSLColor = new HSLColor(angle, color.Saturation, color.Lightness);
                }
                else if (InSquare(mousepoint))
                {
                    mousesquarepoint = mousepoint;
                    SelectedHSLColor = new HSLColor(color.Hue, 1-(mousepoint.X-square.X+1)/square.Width, 1-(mousepoint.Y-square.Y)/square.Height);
                }
                else
                    return;
            }
    }
    }
      

  4.   

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Drawing;namespace ColorPicker
    {
    public struct HSLColor 
    {
    double m_hue;
    double m_saturation;
    double m_lightness; public double Hue
    {
    get { return m_hue; }
    set { m_hue = value; }
    }
    public double Saturation
    {
    get { return m_saturation; }
    set { m_saturation = value; }
    }
    public double Lightness
    {
    get { return m_lightness; }
    set

    m_lightness = value;
    if (m_lightness < 0)
    m_lightness = 0;
    if (m_lightness > 1)
    m_lightness = 1;
    }
    }
    public HSLColor(double hue, double saturation, double lightness)
    {
    m_hue = Math.Min(360, hue);
    m_saturation = Math.Min(1, saturation);
    m_lightness = Math.Min(1, lightness);
    }
    public HSLColor(Color color)
    {
    m_hue = 0;
    m_saturation = 1;
    m_lightness = 1;
    FromRGB(color);
    }
    public Color Color
    {
    get { return ToRGB(); }
    set { FromRGB(value); }
    }
    void FromRGB(Color cc)
    {
    double r = (double)cc.R / 255d;
    double g = (double)cc.G / 255d;
    double b = (double)cc.B / 255d;

    double min = Math.Min(Math.Min(r, g), b);
    double max = Math.Max(Math.Max(r, g), b);
    // calulate hue according formula given in
    // "Conversion from RGB to HSL or HSV"
    m_hue = 0;
    if (min != max)
    {
    if (r == max && g >= b)
    {
    m_hue = 60 * ((g - b) / (max - min)) + 0;
    }
    else
    if (r == max && g < b)
    {
    m_hue = 60 * ((g - b) / (max - min)) + 360;
    }
    else
    if (g == max)
    {
    m_hue = 60 * ((b - r) / (max - min)) + 120;
    }
    else
    if (b == max)
    {
    m_hue = 60 * ((r - g) / (max - min)) + 240;
    }
    }
    // find lightness
    m_lightness = (min+max)/2; // find saturation
    if (m_lightness == 0 ||min == max)
    m_saturation = 0;
    else
    if (m_lightness > 0 && m_lightness <= 0.5)
    m_saturation = (max-min)/(2*m_lightness);
    else
    if (m_lightness > 0.5)
    m_saturation = (max-min)/(2-2*m_lightness);
    }
    Color ToRGB()
    {
    // convert to RGB according to
    // "Conversion from HSL to RGB" double r = m_lightness;
    double g = m_lightness;
    double b = m_lightness;
    if (m_saturation == 0)
    return Color.FromArgb(255,0,0,0); double q ;
    if (m_lightness < 0.5)
    q = m_lightness * (1 + m_saturation);
    else
    q = m_lightness + m_saturation - (m_lightness * m_saturation);
    double p = 2 * m_lightness - q;
    double hk = m_hue / 360; // r,g,b colors
    double[] tc = new double[3] { hk + (1d/3d), hk, hk-(1d/3d)};
    double[] colors = new double[3] {0, 0, 0}; for (int color = 0; color < colors.Length; color++)
    {
    if (tc[color] < 0)
    tc[color] += 1;
    if (tc[color] > 1)
    tc[color] -= 1; if (tc[color] < (1d/6d))
    colors[color] = p + ((q-p)*6*tc[color]);
    else
    if (tc[color] >= (1d/6d) && tc[color] < (1d/2d))
    colors[color] = q;
    else
    if (tc[color] >= (1d/2d) && tc[color] < (2d/3d))
    colors[color] = p + ((q-p)*6*(2d/3d - tc[color]));
    else
    colors[color] = p; colors[color] *= 255; // convert to value expected by Color
    }
    return Color.FromArgb(255, (int)colors[0], (int)colors[1], (int)colors[2]);
    }        public static bool operator ==(HSLColor left, HSLColor right)
            {
                return (left.Hue == right.Hue &&
                        left.Lightness == right.Lightness &&
                        left.Saturation == right.Saturation);
            }
    public static bool operator != (HSLColor left, HSLColor right)
    {
    return !(left == right);
    }        public override bool Equals(object obj)
            {
                if (obj == null || GetType() != obj.GetType()) return false;            return (this == (HSLColor)obj);
            }        public override int GetHashCode()
            {
                return Hue.GetHashCode() ^ Saturation.GetHashCode() ^ Lightness.GetHashCode();
            } public override string ToString()
    {
    string s = string.Format("HSL({0:f2}, {1:f2}, {2:f2})", Hue, Saturation, Lightness);
    return s;
    }
    }
    }
      

  5.   

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    using System.Reflection;
    namespace ColorPicker
    {
        class ImagesUtil
        {
            static public ImageList GetToolbarImageList(Type type, string resourceName, Size imageSize, Color transparentColor)
            {
                System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(type, resourceName);
                ImageList imageList = new ImageList();
                imageList.ImageSize = imageSize;
                imageList.TransparentColor = transparentColor;
                imageList.Images.AddStrip(bitmap);
                imageList.ColorDepth = ColorDepth.Depth24Bit;
                return imageList;
            }
        }    class SelectorImages
        {
            public enum eIndexes
            {
                Right,
                Left,
                Up,
                Down,
                Donut,
            }        static private ImageList m_imageList = null;
            static public ImageList ImageList()
            {
                Type t = typeof(SelectorImages);
                if (m_imageList == null)
                    m_imageList = ImagesUtil.GetToolbarImageList(t, "Resources.colorbarIndicators.bmp", new Size(12, 12), Color.Magenta);
                return m_imageList;
            }
            static public Image Image(eIndexes index)
            {
                return ImageList().Images[(int)index];
            }
        }    class Util
        {
            public static Rectangle Rect(RectangleF rf)
            {
                Rectangle r = new Rectangle();
                r.X = (int)rf.X;
                r.Y = (int)rf.Y;
                r.Width = (int)rf.Width;
                r.Height = (int)rf.Height;
                return r;
            }
            public static RectangleF Rect(Rectangle r)
            {
                RectangleF rf = new RectangleF();
                rf.X = (float)r.X;
                rf.Y = (float)r.Y;
                rf.Width = (float)r.Width;
                rf.Height = (float)r.Height;
                return rf;
            }
            public static Point Point(PointF pf)
            {
                return new Point((int)pf.X, (int)pf.Y);
            }
            public static PointF Center(RectangleF r)
            {
                PointF center = r.Location;
                center.X += r.Width / 2;
                center.Y += r.Height / 2;
                return center;
            }        public static void DrawFrame(Graphics dc, RectangleF r, float cornerRadius, Color color)
            {
                Pen pen = new Pen(color);
                if (cornerRadius <= 0)
                {
                    dc.DrawRectangle(pen, Rect(r));
                    return;
                }
                cornerRadius = (float)Math.Min(cornerRadius, Math.Floor(r.Width) - 2);
                cornerRadius = (float)Math.Min(cornerRadius, Math.Floor(r.Height) - 2);            GraphicsPath path = new GraphicsPath();
                path.AddArc(r.X, r.Y, cornerRadius, cornerRadius, 180, 90);
                path.AddArc(r.Right - cornerRadius, r.Y, cornerRadius, cornerRadius, 270, 90);
                path.AddArc(r.Right - cornerRadius, r.Bottom - cornerRadius, cornerRadius, cornerRadius, 0, 90);
                path.AddArc(r.X, r.Bottom - cornerRadius, cornerRadius, cornerRadius, 90, 90);
                path.CloseAllFigures();
                dc.DrawPath(pen, path);
            }
        }
    }
      

  6.   

    TOHSL  和 TORGB存在很多优化空间,  其实这个东西主要看创意,  谢谢共享。
      

  7.   

    下载地址http://download.csdn.net/detail/zhuankeshumo/5057032
    传上去2天后终于显示出来了    延迟了两天
      

  8.   

    Hot Virtual Keyboard                
      

  9.   

    Hot Virtual Keyboard !!!!!!!!
      

  10.   

    Hot Virtual Keyboard !!!!!!!!