C # conversion between RGB and HSB,

Source: Internet
Author: User

C # conversion between RGB and HSB,
Background

In a recent project, you can set any color to obtain two colors: Deep and light. That is to say, the two colors of the same color are used. The first thing that comes to mind is to adjust transparency, but the effect is not ideal. Later, I tried to adjust the color brightness and found that this is the correct solution. However, in. NET, the brightness of the Color cannot be directly changed. You need to convert the Color to the HSB mode, and then change the value of B to adjust the brightness. After adjusting the brightness, you need to switch to the familiar RGB mode. The following shows the color conversion method.

 

Code
1 /// <summary> 2 // color conversion help class 3 /// </summary> 4 public class ColorConversionHelper 5 {6 /// <summary> 7 /// convert RGB to HSB 8 /// </summary> 9 /// <param name = "red"> red value </param> 10 /// <param name = "green"> green value </param> 11 /// <param name = "blue"> blue value </param> 12 /// <returns> return: HSB value set </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 // convert HSB to RGB26 /// </summary> 27 /// <param name = "hue"> tone </ param> 28 // <param name = "saturation"> saturation </param> 29 // <param name = "brightness"> brightness </param> 30 /// <returns> return: 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 else39 {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); 49g = (int) (t * 255.0f + 0.5f); 50 B = (int) (p * 255.0f + 0.5f); 51 break; 52 case :53 r = (int) (q * 255.0f + 0.5f); 54g = (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); 59g = (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); 64g = (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); 69g = (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); 74g = (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}

 

 

Additional reading

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

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.