Conversion from the RGB-representation to the HSV-representation. The tested values are the reverse values from the hsv_to_rgb-doctests. Function "approximately_equal_hsv" is needed because of small deviations due to rounding for the RGB-values. >>> approximately_equal_hsv(rgb_
(red: int, green: int, blue: int)
| 82 | |
| 83 | |
| 84 | def rgb_to_hsv(red: int, green: int, blue: int) -> list[float]: |
| 85 | """ |
| 86 | Conversion from the RGB-representation to the HSV-representation. |
| 87 | The tested values are the reverse values from the hsv_to_rgb-doctests. |
| 88 | Function "approximately_equal_hsv" is needed because of small deviations due to |
| 89 | rounding for the RGB-values. |
| 90 | |
| 91 | >>> approximately_equal_hsv(rgb_to_hsv(0, 0, 0), [0, 0, 0]) |
| 92 | True |
| 93 | >>> approximately_equal_hsv(rgb_to_hsv(255, 255, 255), [0, 0, 1]) |
| 94 | True |
| 95 | >>> approximately_equal_hsv(rgb_to_hsv(255, 0, 0), [0, 1, 1]) |
| 96 | True |
| 97 | >>> approximately_equal_hsv(rgb_to_hsv(255, 255, 0), [60, 1, 1]) |
| 98 | True |
| 99 | >>> approximately_equal_hsv(rgb_to_hsv(0, 255, 0), [120, 1, 1]) |
| 100 | True |
| 101 | >>> approximately_equal_hsv(rgb_to_hsv(0, 0, 255), [240, 1, 1]) |
| 102 | True |
| 103 | >>> approximately_equal_hsv(rgb_to_hsv(255, 0, 255), [300, 1, 1]) |
| 104 | True |
| 105 | >>> approximately_equal_hsv(rgb_to_hsv(64, 128, 128), [180, 0.5, 0.5]) |
| 106 | True |
| 107 | >>> approximately_equal_hsv(rgb_to_hsv(193, 196, 224), [234, 0.14, 0.88]) |
| 108 | True |
| 109 | >>> approximately_equal_hsv(rgb_to_hsv(128, 32, 80), [330, 0.75, 0.5]) |
| 110 | True |
| 111 | """ |
| 112 | if red < 0 or red > 255: |
| 113 | raise Exception("red should be between 0 and 255") |
| 114 | |
| 115 | if green < 0 or green > 255: |
| 116 | raise Exception("green should be between 0 and 255") |
| 117 | |
| 118 | if blue < 0 or blue > 255: |
| 119 | raise Exception("blue should be between 0 and 255") |
| 120 | |
| 121 | float_red = red / 255 |
| 122 | float_green = green / 255 |
| 123 | float_blue = blue / 255 |
| 124 | value = max(float_red, float_green, float_blue) |
| 125 | chroma = value - min(float_red, float_green, float_blue) |
| 126 | saturation = 0 if value == 0 else chroma / value |
| 127 | |
| 128 | if chroma == 0: |
| 129 | hue = 0.0 |
| 130 | elif value == float_red: |
| 131 | hue = 60 * (0 + (float_green - float_blue) / chroma) |
| 132 | elif value == float_green: |
| 133 | hue = 60 * (2 + (float_blue - float_red) / chroma) |
| 134 | else: |
| 135 | hue = 60 * (4 + (float_red - float_green) / chroma) |
| 136 | |
| 137 | hue = (hue + 360) % 360 |
| 138 | |
| 139 | return [hue, saturation, value] |
| 140 | |
| 141 |
nothing calls this directly
no outgoing calls
no test coverage detected