Conversion formula between RGB and HSB

Source: Internet
Author: User

let's start by understanding some concepts:1.RGB is an additive color model that mixes different proportions of red/green/blue together to get a new color. The RGB color model is typically represented as:

2.HSB (HSV) expresses color through three elements of hue/saturation/brightness.

H (Hue): Indicates the type of color (for example, red, green, or yellow). The value range is 0-360. Each of these values represents a color.

S (saturation): saturation of the color. from 0 to 1. Sometimes also known as purity. (0 for grayscale, 1 for pure color)

B (brightness or Value): the degree of brightness of the color. from 0 to 1. (0 for Black, 1 for a specific saturation color)

The back address is an online tool for observing RGB to HSB conversions: http://web.bentley.edu/empl/c/ncarter/MA307/color-converter.html

It is convenient to use RGB to represent colors, but the RGB values of the two similar colors may be 108,000 of a difference. Using HSV (hue hue, saturation saturation, Value (brightness) lightness, or HSB) to represent colors is more in line with people's habits.

Conversion of RGB to HSV (HSB):


The conversion of the HSV (HSB) to RGB:


According to the above instructions, there are the following conversion formulas (Java code):

[Java] view Plaincopy
  1. public static float[] RGB2HSB (int rgbr, int rgbg, int rgbb) {
  2. Assert 0 <= rgbr && rgbr <= 255;
  3. Assert 0 <= rgbg && rgbg <= 255;
  4. Assert 0 <= rgbb && rgbb <= 255;
  5. int[] RGB = new int[] {rgbr, RGBG, RGBB};
  6. Arrays.sort (RGB);
  7. int max = rgb[2];
  8. int min = rgb[0];
  9. float HSBB = max/255.0f;
  10. float Hsbs = max = = 0? 0: (max-min)/(float) max;
  11. float HSBH = 0;
  12. if (max = = Rgbr && rgbg >= rgbb) {
  13. HSBH = (RGBG-RGBB) * 60f/(Max-min) + 0;
  14. } else if (max = = Rgbr && RGBG < RGBB) {
  15. HSBH = (RGBG-RGBB) * 60f/(max-min) + 360;
  16. } else if (max = = RGBG) {
  17. HSBH = (RGBB-RGBR) * 60f/(max-min) + 120;
  18. } else if (max = = RGBB) {
  19. HSBH = (RGBR-RGBG) * 60f/(max-min) + 240;
  20. }
  21. return new float[] {HSBH, Hsbs, HSBB};
  22. }
  23. public static int[] Hsb2rgb (float h, float s, float v) {
  24. Assert Float.compare (H, 0.0f) >= 0 && Float.compare (H, 360.0f) <= 0;
  25. Assert Float.compare (S, 0.0f) >= 0 && Float.compare (S, 1.0f) <= 0;
  26. Assert Float.compare (V, 0.0f) >= 0 && float.compare (V, 1.0f) <= 0;
  27. float r = 0, g = 0, b = 0;
  28. int i = (int) ((H/60)% 6);
  29. float f = (h/60)-I;
  30. float p = v * (1-s);
  31. float q = v * (1-F * s);
  32. float T = v * (1-(1-f) * s);
  33. switch (i) {
  34. Case 0:
  35. R = V;
  36. g = t;
  37. b = P;
  38. Break
  39. Case 1:
  40. R = q;
  41. g = V;
  42. b = P;
  43. Break
  44. Case 2:
  45. r = P;
  46. g = V;
  47. b = t;
  48. Break
  49. Case 3:
  50. r = P;
  51. g = q;
  52. b = V;
  53. Break
  54. Case 4:
  55. R = t;
  56. g = P;
  57. b = V;
  58. Break
  59. Case 5:
  60. R = V;
  61. g = P;
  62. b = q;
  63. Break
  64. Default
  65. Break
  66. }
  67. return new int[] {(int) (R * 255.0), (int) (g * 255.0),
  68. (int) (b * 255.0)};
  69. }




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Conversion formula between RGB and HSB

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.