Commit 0155780
committed
Fix division by zero bug in colorsys.rgb_to_hls() for near-white colors
Fixes gh-106498: Prevent ZeroDivisionError when converting RGB values
very close to white (e.g., (0.9999999999999999, 1, 1)) to HLS.
Changes:
- Add numerical stability check in rgb_to_hls() function
- When denominator (2.0 - maxc - minc) is very close to zero, set s = 0.0
- Improve HSV hue handling with explicit normalization
- Add test case for hue wrapping edge cases
This fix prevents crashes when converting colors very close to pure white
while maintaining backward compatibility and mathematical correctness.1 parent 9df477c commit 0155780
2 files changed
+31
-2
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
83 | 83 | | |
84 | 84 | | |
85 | 85 | | |
86 | | - | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
87 | 92 | | |
88 | 93 | | |
89 | 94 | | |
| |||
145 | 150 | | |
146 | 151 | | |
147 | 152 | | |
148 | | - | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
149 | 156 | | |
150 | 157 | | |
151 | 158 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
106 | 106 | | |
107 | 107 | | |
108 | 108 | | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
109 | 131 | | |
110 | 132 | | |
0 commit comments