# Program to Change RGB color model to HSV color model

Given RGB color range, our task is to convert RGB color to HSV color.

**RGB Color Model :**

The RGB color model is an additive color model in which red, green and blue light are added together in various ways to reproduce a broad array of colors. The name of the model comes from the initials of the three additive primary colors, red, green, and blue.

**HSV Color Model :**

HSV – (hue, saturation, value), also known as HSB (hue, saturation, brightness), is often used by artists because it is often more natural to think about a color in terms of hue and saturation than in terms of additive or subtractive color components. HSV is a transformation of an RGB colorspace, and its components and colorimetry are relative to the RGB colorspace from which it was derived.

**Examples :**

Input :r, g, b = 45, 215, 0Output :h, s, v = 107.44186046511628, 100.0, 84.31372549019608Input :r, g, v = 31, 52, 29Output :h, s, v = 114.78260869565217, 44.230769230769226, 20.392156862745097

Approach :

- Divide r, g, b by 255
- Compute cmax, cmin, difference
- Hue calculation :

- if cmax and cmin equal 0, then h = 0
- if cmax equal r then compute h = (60 * ((g – b) / diff) + 360) % 360
- if cmax equal g then compute h = (60 * ((b – r) / diff) + 120) % 360
- if cmax equal b then compute h = (60 * ((r – g) / diff) + 240) % 360
- Saturation computation :

- if cmax = 0, then s = 0
- if cmax does not equal 0 then compute s = (diff/cmax)*100
- Value computation :

- v = cmax*100

Below is the implementation of above approach :

`# Python3 program change RGB Color ` `# Model to HSV Color Model ` ` ` `def` `rgb_to_hsv(r, g, b): ` ` ` ` ` `# R, G, B values are divided by 255 ` ` ` `# to change the range from 0..255 to 0..1: ` ` ` `r, g, b ` `=` `r ` `/` `255.0` `, g ` `/` `255.0` `, b ` `/` `255.0` ` ` ` ` `# h, s, v = hue, saturation, value ` ` ` `cmax ` `=` `max` `(r, g, b) ` `# maximum of r, g, b ` ` ` `cmin ` `=` `min` `(r, g, b) ` `# minimum of r, g, b ` ` ` `diff ` `=` `cmax` `-` `cmin ` `# diff of cmax and cmin. ` ` ` ` ` `# if cmax and cmax are equal then h = 0 ` ` ` `if` `cmax ` `=` `=` `cmin: ` ` ` `h ` `=` `0` ` ` ` ` `# if cmax equal r then compute h ` ` ` `elif` `cmax ` `=` `=` `r: ` ` ` `h ` `=` `(` `60` `*` `((g ` `-` `b) ` `/` `diff) ` `+` `360` `) ` `%` `360` ` ` ` ` `# if cmax equal g then compute h ` ` ` `elif` `cmax ` `=` `=` `g: ` ` ` `h ` `=` `(` `60` `*` `((b ` `-` `r) ` `/` `diff) ` `+` `120` `) ` `%` `360` ` ` ` ` `# if cmax equal b then compute h ` ` ` `elif` `cmax ` `=` `=` `b: ` ` ` `h ` `=` `(` `60` `*` `((r ` `-` `g) ` `/` `diff) ` `+` `240` `) ` `%` `360` ` ` ` ` `# if cmax equal zero ` ` ` `if` `cmax ` `=` `=` `0` `: ` ` ` `s ` `=` `0` ` ` `else` `: ` ` ` `s ` `=` `(diff ` `/` `cmax) ` `*` `100` ` ` ` ` `# compute v ` ` ` `v ` `=` `cmax ` `*` `100` ` ` `return` `h, s, v ` ` ` ` ` `''' Driver Code '''` `# print(rgb_to_hsv(45, 215, 0)) ` `# print(rgb_to_hsv(31, 52, 29)) ` ` ` `print` `(rgb_to_hsv(` `129` `, ` `88` `, ` `47` `)) ` |

*chevron_right*

*filter_none*

**Output :**

(30.0, 63.56589147286821, 50.588235294117645)

## Recommended Posts:

- Change an HTML5 input placeholder color with CSS
- Color N boxes using M colors such that K boxes have different color from the box on its left
- CSS | border-top-color Property
- CSS | outline-color Property
- CSS | border-color Property
- CSS | column-rule-color Property
- Minimum steps to color the tree with given colors
- Erdos Renyl Model (for generating Random Graphs)
- Ways to color a skewed tree such that parent and child have different colors
- Coin Change | DP-7
- Overall percentage change from successive changes
- Change K elements so that (a1^2 + a2^2 + …+ aN^2 ) <= (a1 + a2 +…+ aN) becomes true
- Coin Change | BFS Approach
- Change all even bits in a number to 0
- Change string to a new character set

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.