2017-03-01 146 views
-3

對於我的任務我需要做一個顏色選擇器,看起來像這樣:HSV三角形C#

img

我已經得到了車輪部,但無法弄清楚如何繪製三角形。

+0

大概['DrawImage'](https://msdn.microsoft.com/en-us/library/558kfzex)應該足夠了。 –

+0

您可以預先製作一個半透明的三角形,然後在純色上繪製。 –

+2

你有沒有嘗試過任何這個呢?如果是的話請添加你的代碼.. – Darshak

回答

2
using System.Drawing; 
using System.Drawing.Imaging; 

Image DrawColorPicker() 
{ 
    const int SIZE = 400; 
    var img = new Bitmap(SIZE, SIZE, PixelFormat.Format32bppArgb); 
    var centerX = SIZE/2; 
    var centerY = SIZE/2; 
    var innerRadius = SIZE * 5/12; 
    var outerRadius = SIZE/2; 
    for (int y = 0; y < SIZE; y++) 
    { 
     for (int x = 0; x < SIZE; x++) 
     { 
      Color color; 
      var distanceFromCenter = Math.Sqrt((x - centerX) * (x - centerX) + (y - centerY) * (y - centerY)); 
      var angle = Math.Atan2(y - centerY, x - centerX) + Math.PI/2; 
      if (angle < 0) angle += 2 * Math.PI; 
      var sqrt3 = Math.Sqrt(3); 
      // Outside 
      if (distanceFromCenter > outerRadius) 
      { 
       color = Color.Transparent; 
      } 
      else if (distanceFromCenter > innerRadius) 
      { 
       // Wheel 
       var hue = angle; 
       var sat = 1.0; // Could use selected saturation and value, instead of ones 
       var val = 1.0; 
       color = HSV(hue, sat, val, 1); 
      } 
      else 
      { 
       // Inside 
       var x1 = (x - centerX) * 1.0/innerRadius; 
       var y1 = (y - centerY) * 1.0/innerRadius; 
       if (  0 * x1 + 2 * y1 > 1) color = Color.Transparent; 
       else if (sqrt3 * x1 + (-1) * y1 > 1) color = Color.Transparent; 
       else if (-sqrt3 * x1 + (-1) * y1 > 1) color = Color.Transparent; 
       else 
       { 
        // Triangle 
        var hue = 0.0; // Could use the selected hue, instead of zero 
        var sat = (1 - 2 * y1)/(sqrt3 * x1 - y1 + 2); 
        var val = (sqrt3 * x1 - y1 + 2)/3; 

        color = HSV(hue, sat, val, 1); 
       } 
      } 
      img.SetPixel(x, y, color); 
     } 
    } 

    return img; 
} 

Color HSV(double hue, double sat, double val, double alpha) 
{ 
    var chroma = val * sat; 
    var step = Math.PI/3; 
    var interm = chroma * (1 - Math.Abs((hue/step) % 2.0 - 1)); 
    var shift = val - chroma; 
    if (hue < 1 * step) return RGB(shift + chroma, shift + interm, shift + 0,  alpha); 
    if (hue < 2 * step) return RGB(shift + interm, shift + chroma, shift + 0,  alpha); 
    if (hue < 3 * step) return RGB(shift + 0,  shift + chroma, shift + interm, alpha); 
    if (hue < 4 * step) return RGB(shift + 0,  shift + interm, shift + chroma, alpha); 
    if (hue < 5 * step) return RGB(shift + interm, shift + 0,  shift + chroma, alpha); 
    return      RGB(shift + chroma, shift + 0,  shift + interm, alpha); 
} 

Color RGB(double red, double green, double blue, double alpha) 
{ 
    return Color.FromArgb(
     Math.Min(255, (int)(alpha * 256)), 
     Math.Min(255, (int)(red * 256)), 
     Math.Min(255, (int)(green * 256)), 
     Math.Min(255, (int)(blue * 256))); 
} 

結果:

Color picker

+0

這有效,謝謝你! –