MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / hsv_to_rgb

Function hsv_to_rgb

conversions/rgb_hsv_conversion.py:15–81  ·  view source on GitHub ↗

Conversion from the HSV-representation to the RGB-representation. Expected RGB-values taken from https://www.rapidtables.com/convert/color/hsv-to-rgb.html >>> hsv_to_rgb(0, 0, 0) [0, 0, 0] >>> hsv_to_rgb(0, 0, 1) [255, 255, 255] >>> hsv_to_rgb(0, 1, 1) [255, 0,

(hue: float, saturation: float, value: float)

Source from the content-addressed store, hash-verified

13
14
15def hsv_to_rgb(hue: float, saturation: float, value: float) -> list[int]:
16 """
17 Conversion from the HSV-representation to the RGB-representation.
18 Expected RGB-values taken from
19 https://www.rapidtables.com/convert/color/hsv-to-rgb.html
20
21 >>> hsv_to_rgb(0, 0, 0)
22 [0, 0, 0]
23 >>> hsv_to_rgb(0, 0, 1)
24 [255, 255, 255]
25 >>> hsv_to_rgb(0, 1, 1)
26 [255, 0, 0]
27 >>> hsv_to_rgb(60, 1, 1)
28 [255, 255, 0]
29 >>> hsv_to_rgb(120, 1, 1)
30 [0, 255, 0]
31 >>> hsv_to_rgb(240, 1, 1)
32 [0, 0, 255]
33 >>> hsv_to_rgb(300, 1, 1)
34 [255, 0, 255]
35 >>> hsv_to_rgb(180, 0.5, 0.5)
36 [64, 128, 128]
37 >>> hsv_to_rgb(234, 0.14, 0.88)
38 [193, 196, 224]
39 >>> hsv_to_rgb(330, 0.75, 0.5)
40 [128, 32, 80]
41 """
42 if hue < 0 or hue > 360:
43 raise Exception("hue should be between 0 and 360")
44
45 if saturation < 0 or saturation > 1:
46 raise Exception("saturation should be between 0 and 1")
47
48 if value < 0 or value > 1:
49 raise Exception("value should be between 0 and 1")
50
51 chroma = value * saturation
52 hue_section = hue / 60
53 second_largest_component = chroma * (1 - abs(hue_section % 2 - 1))
54 match_value = value - chroma
55
56 if hue_section >= 0 and hue_section <= 1:
57 red = round(255 * (chroma + match_value))
58 green = round(255 * (second_largest_component + match_value))
59 blue = round(255 * (match_value))
60 elif hue_section > 1 and hue_section <= 2:
61 red = round(255 * (second_largest_component + match_value))
62 green = round(255 * (chroma + match_value))
63 blue = round(255 * (match_value))
64 elif hue_section > 2 and hue_section <= 3:
65 red = round(255 * (match_value))
66 green = round(255 * (chroma + match_value))
67 blue = round(255 * (second_largest_component + match_value))
68 elif hue_section > 3 and hue_section <= 4:
69 red = round(255 * (match_value))
70 green = round(255 * (second_largest_component + match_value))
71 blue = round(255 * (chroma + match_value))
72 elif hue_section > 4 and hue_section <= 5:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected