C # RGB and HSB convert each other

Source: Internet
Author: User

background

In the recent project, there is a scene in which you set any color to get the two colors that are darker and lighter. In other words, the color is the same as the shades of two colors. The first thought is to adjust the transparency, but the effect is not ideal. Later try to adjust the color brightness, found that this is the positive solution. But. NET can not directly change the brightness of color, you need to convert color to HSB mode, and then change the value of B to adjust the brightness. After adjusting the brightness, we need to convert it to our familiar RGB mode to use. The color conversion method is given below.

Code
1     /// <summary>2     ///Color Conversion Help class3     /// </summary>4      Public classColorconversionhelper5     {6         /// <summary>7         ///RGB to HSB8         /// </summary>9         /// <param name= "Red" >Red Value</param>Ten         /// <param name= "green" >Green Value</param> One         /// <param name= "Blue" >Blue Value</param> A         /// <returns>return: HSB Value collection</returns> -          Public Staticlist<float> RGBTOHSB (intRedintGreenintBlue) -         { thelist<float> hsblist =Newlist<float>(); -System.Drawing.Color Dcolor =System.Drawing.Color.FromArgb (red, green, blue); - Hsblist.add (Dcolor.gethue ()); - Hsblist.add (Dcolor.getsaturation ()); + Hsblist.add (Dcolor.getbrightness ()); -             returnhsblist; +         } A  at  -         /// <summary> -         ///HSB Turn RGB -         /// </summary> -         /// <param name= "Hue" >Hue</param> -         /// <param name= "saturation" >Saturation Level</param> in         /// <param name= "Brightness" >Brightness</param> -         /// <returns>return: Color</returns> to          Public StaticColor Hsbtorgb (floatHuefloatSaturation,floatbrightness) +         { -             intR =0, G =0, B =0; the             if(Saturation = =0) *             { $r = G = b = (int) (Brightness *255.0f+0.5f);Panax Notoginseng             } -             Else the             { +                 floatH = (Hue-(float) Math.floor (hue)) *6.0f; A                 floatF = h-(float) Math.floor (h); the                 floatp = Brightness * (1.0f-saturation); +                 floatQ = Brightness * (1.0f-Saturation *f); -                 floatt = Brightness * (1.0f-(Saturation * (1.0f-f))); $                 Switch((int) h) $                 { -                      Case 0: -R = (int) (Brightness *255.0f+0.5f); theg = (int) (T *255.0f+0.5f); -B = (int) (P *255.0f+0.5f);Wuyi                          Break; the                      Case 1: -R = (int) (Q *255.0f+0.5f); Wug = (int) (Brightness *255.0f+0.5f); -B = (int) (P *255.0f+0.5f); About                          Break; $                      Case 2: -R = (int) (P *255.0f+0.5f); -g = (int) (Brightness *255.0f+0.5f); -B = (int) (T *255.0f+0.5f); A                          Break; +                      Case 3: theR = (int) (P *255.0f+0.5f); -g = (int) (Q *255.0f+0.5f); $B = (int) (Brightness *255.0f+0.5f); the                          Break; the                      Case 4: theR = (int) (T *255.0f+0.5f); theg = (int) (P *255.0f+0.5f); -B = (int) (Brightness *255.0f+0.5f); in                          Break; the                      Case 5: theR = (int) (Brightness *255.0f+0.5f); Aboutg = (int) (P *255.0f+0.5f); theB = (int) (Q *255.0f+0.5f); the                          Break; the                 } +             } -             returnColor.FromArgb (Convert.tobyte (255), Convert.tobyte (R), Convert.tobyte (g), Convert.tobyte (b)); the         }Bayi}

Extended Reading

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

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

C # RGB and HSB convert each other

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.