程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C# RGB和HSB相互轉換,

C# RGB和HSB相互轉換,

編輯:C#入門知識

C# RGB和HSB相互轉換,


背景 

    最近做的項目中有這樣一個場景,設置任意一種顏色,得到這種顏色偏深和偏淺的兩種顏色。也就是說取該顏色同色系的深淺兩種顏色。首先想到的是調節透明度,但效果不理想。後來嘗試調節顏色亮度,發現這才是正解。但是.NET中不能直接改變Color的亮度,需要將Color轉換成HSB模式,然後改變B的值調節亮度。調節亮度後需要再轉換成我們熟悉的RGB模式才能使用。下面給出顏色轉換方法。

 

代碼 

 1     /// <summary>
 2     /// 顏色轉換幫助類
 3     /// </summary>
 4     public class ColorConversionHelper
 5     {
 6         /// <summary>
 7         ///  RGB轉HSB
 8         /// </summary>
 9         /// <param name="red">紅色值</param>
10         /// <param name="green">綠色值</param>
11         /// <param name="blue">藍色值</param>
12         /// <returns>返回:HSB值集合</returns>
13         public static List<float> RGBtoHSB(int red, int green, int blue)
14         {
15             List<float> hsbList = new List<float>();
16             System.Drawing.Color dColor = System.Drawing.Color.FromArgb(red, green, blue);
17             hsbList.Add(dColor.GetHue());
18             hsbList.Add(dColor.GetSaturation());
19             hsbList.Add(dColor.GetBrightness());
20             return hsbList;
21         }
22 
23 
24         /// <summary>
25         /// HSB轉RGB
26         /// </summary>
27         /// <param name="hue">色調</param>
28         /// <param name="saturation">飽和度</param>
29         /// <param name="brightness">亮度</param>
30         /// <returns>返回:Color</returns>
31         public static Color HSBtoRGB(float hue, float saturation, float brightness)
32         {
33             int r = 0, g = 0, b = 0;
34             if (saturation == 0)
35             {
36                 r = g = b = (int)(brightness * 255.0f + 0.5f);
37             }
38             else
39             {
40                 float h = (hue - (float)Math.Floor(hue)) * 6.0f;
41                 float f = h - (float)Math.Floor(h);
42                 float p = brightness * (1.0f - saturation);
43                 float q = brightness * (1.0f - saturation * f);
44                 float t = brightness * (1.0f - (saturation * (1.0f - f)));
45                 switch ((int)h)
46                 {
47                     case 0:
48                         r = (int)(brightness * 255.0f + 0.5f);
49                         g = (int)(t * 255.0f + 0.5f);
50                         b = (int)(p * 255.0f + 0.5f);
51                         break;
52                     case 1:
53                         r = (int)(q * 255.0f + 0.5f);
54                         g = (int)(brightness * 255.0f + 0.5f);
55                         b = (int)(p * 255.0f + 0.5f);
56                         break;
57                     case 2:
58                         r = (int)(p * 255.0f + 0.5f);
59                         g = (int)(brightness * 255.0f + 0.5f);
60                         b = (int)(t * 255.0f + 0.5f);
61                         break;
62                     case 3:
63                         r = (int)(p * 255.0f + 0.5f);
64                         g = (int)(q * 255.0f + 0.5f);
65                         b = (int)(brightness * 255.0f + 0.5f);
66                         break;
67                     case 4:
68                         r = (int)(t * 255.0f + 0.5f);
69                         g = (int)(p * 255.0f + 0.5f);
70                         b = (int)(brightness * 255.0f + 0.5f);
71                         break;
72                     case 5:
73                         r = (int)(brightness * 255.0f + 0.5f);
74                         g = (int)(p * 255.0f + 0.5f);
75                         b = (int)(q * 255.0f + 0.5f);
76                         break;
77                 }
78             }
79             return Color.FromArgb(Convert.ToByte(255), Convert.ToByte(r), Convert.ToByte(g), Convert.ToByte(b));
80         }
81     }

 

 

擴展閱讀

http://stackoverflow.com/questions/4106363/converting-rgb-to-hsb-colors

http://my.oschina.net/soitravel/blog/79862

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved