Color IX in computer -- Summary: Fast conversion of RGB and HSV

Source: Internet
Author: User

Write a section about the colorArticle, Review the past several articles and make a staged summary. Based on the experiences of the previous articles, the formulas for fast conversion of RGB and HSV modes are derived.

RGB mode: The color is represented by the components of R, G, and B. The value range of the component is [0,255], and the integer type is

HSV mode: The color is represented by three components: H, S, and V.

Component H: The color of the color. The value ranges from 0,360 to an integer.

Component S: indicates the color purity. value range: [0, 1), floating point type.

Component V: The brightness of the color. value range: [0, 1), floating point

For more information, see the color III--HSV representation in the computer"

1. RGB to HSV

How to quickly convert colors (R, G, B) to (H, S, V)

See the article "Computer Color IV-solid color, color phase color", "Computer Color V-quick calculation of color phase value"

Make Max the maximum values of the three components R, G, and B; min the minimum values of the three components.

If max = min, then

H = 0

S = 0

V = max/255

If Max =min

When G is greater than or equal to B

H = (max-r '+ G'-min + B'-min)/(max-min) × 60

S = 1-min/MAX

V = max/255

When G <B

H = 360-(Max-R' + G'-min + B '-min)/(max-min) × 60

S = 1-min/MAX

V = max/255

2. HSV to RGB

How to quickly convert colors (H, S, v) to (R, G, B)

See article "color VIII in computer-quick calculation of color deflection"

Color (H, S, v) can be seen as color (0, S, v) clockwise deflection h to get the color, in fact, is to obtain the color Max and Min weight

Max = V x 255 = 255 v

If S = 0, it means max = min, which indicates the gray color.

R = max

G = max

B = max

If S> 0, P = MAX-MIN = 255sv, min = max-P

If H> 180, make H' = 360-h; otherwise, make H' = H

Set λ = H '/60 × P

R = 2 P-λ; if r <0, r = 0; if r> P, R = P;

G = λ; if G> P, G = P;

B = λ-2 p; if B <0; then B = 0;

If H> 180, the values of G and B are exchanged. Otherwise, the values are not exchanged. Last

R = R + min

G = G + min

B = B + min

In summary, the above are the mutual conversion formulas derived from HSV and RGB, which are simpler than the original formulas introduced on the Internet. See the color III--HSV Representation Method in the computer ".

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.