Conversion between RGB space and HSV space (C ++ implementation, correcting most code errors on the Internet)

Source: Internet
Author: User

 

The RGB and HSV spaces are both color space models of the image. In RGB color mode, the RGB model is used to allocate a value of 0 to the RGB component of each pixel in the image ~ The intensity value within the range of 255. RGB Images use only three colors to mix them in different proportions and reproduce the 16777216 colors on the screen. In RGB mode, each RGB component can use a value from 0 (black) to 255 (white. For example, the R value 255, G value 0, and B value 0 are used in bright red. Gray shadows are generated when all three values are equal. When the value of all components is 255, the result is pure white; when the value is 0, the result is pure black. The HSV model is usually used in computer graphics applications. The HSV color wheel is often used when you must select a color to apply to various application environments of specific graphic elements. The color phase is represented as a ring, and an independent triangle can be used to represent the saturation and brightness. Typically, the vertical axis of the triangle indicates the saturation, while the horizontal axis indicates the brightness. In this way, you can select the color first in the ring and select the desired saturation and brightness from the triangle.

 

Another visible method of the HSV model is the cone. In this representation, the color phase is represented as the angle around the center axis of the cone, and the saturation is represented as the distance from the center of the cross section of the cone to this point, brightness is expressed as the distance from the center of the cross section of the cone to the vertex. Some indicate that the six-sided cone is used. This method is more suitable for displaying the HSV color space in a single object. However, due to its three-dimensional nature, it is not suitable for selecting colors on the two-dimensional computer interface.
The HSV color can also be expressed as a cylinder similar to the cone above. The color phases change along the outer circumference of the cylinder, and the saturation changes along the distance from the center of the cross section, the brightness varies along the distance between the end and the top of the cross section. This representation may be considered a more accurate mathematical model of the HSV color space; however, in reality, the degree of saturation and the number of colors that can be distinguished decreases as the brightness approaches black. In addition, the typical computer uses a limited range of precision to store RGB values. This restricts the accuracy, coupled with the limitations of human color perception, so that cone representation is more practical in most cases.
Conversion of two types of space
Conversion from RGB to HSL or HSV
Set (R, G, B) to the red, green, and blue coordinates of a color. Their values are real numbers between 0 and 1. Set max is equivalent to the setter in R, G, and B. Set min to the smallest of these values. We need to find the (H, S, L) value in the HSL space. Here, H in [0,360) is the color angle of the angle, while s, the [0, 1] is the saturation and brightness, and is calculated:

 

Void rgb2hsv (float R, float g, float B, float & H, float & S, float & V) <br/>{< br/> // R, G, B values are from 0 to 1 <br/> // H = [0,360], S = [], V = [0, 1] <br/> // If S = 0, then H =-1 (undefined) <br/> float min, Max, Delta, TMP; <br/> TMP = r> G? G: R; <br/> min = TMP> B? B: TMP; <br/> TMP = r> G? R: G; <br/> max = TMP> B? TMP: B; <br/> V = max; // v <br/> Delta = max-min; <br/> If (max! = 0) <br/> S = delta/max; // S <br/> else <br/> {<br/> // R = G = B = 0/S = 0, V is undefined <br/> S = 0; <br/> H = 0; <br/> return; <br/>}< br/> If (delta = 0) {<br/> H = 0; <br/> return; <br/>}< br/> else if (r = max) {<br/> If (G> = B) <br/> H = (G-B) /delta; // between yellow & magenta <br/> else <br/> H = (G-B)/Delta + 6.0; <br/>}< br/> else if (G = max) <br/> H = 2.0 + (B-r)/delta; // between cyan & yellow <br/> else if (B = max) <br/> H = 4.0 + (r-g)/delta; // between magenta & cyan <br/> H * = 60.0; // degrees <br/>}< br/> 

Conversion from HSV to RGB
Similarly, given a color defined in the values of (H, S, v) in hsv, with the above H, and the S and V that indicate the saturation and brightness change between 0 and 1 respectively. The three primary colors (R, G, B) corresponding to the RGB space can be calculated:

For each color vector (R, G, B ),

 

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.