From 368125907feee3df5d003902ce599027941ab0aa Mon Sep 17 00:00:00 2001 From: manoj Date: Fri, 8 May 2026 21:50:15 +0530 Subject: [PATCH 1/5] Add clipping for all conversion functions in colorsys to maintain validity and consistency --- Lib/colorsys.py | 75 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 73 insertions(+), 2 deletions(-) diff --git a/Lib/colorsys.py b/Lib/colorsys.py index e97f91718a3a30..b9616e66ed075e 100644 --- a/Lib/colorsys.py +++ b/Lib/colorsys.py @@ -7,7 +7,8 @@ All inputs and outputs are triples of floats in the range [0.0...1.0] (with the exception of I and Q, which covers a slightly larger range). -Inputs outside the valid range may cause exceptions or invalid outputs. +Inputs outside the valid range give invalid outputs, Hence the outputs +are clipped by default. Supported color systems: RGB: Red, Green, Blue components @@ -41,6 +42,19 @@ def rgb_to_yiq(r, g, b): y = 0.30*r + 0.59*g + 0.11*b i = 0.74*(r-y) - 0.27*(b-y) q = 0.48*(r-y) + 0.41*(b-y) + + if y < 0.0: + y = 0.0 + if i < -0.5957: + i = -0.5957 + if q < -0.5226: + q = -0.5226 + if y > 1.0: + y = 1.0 + if i > 0.5957: + i = 0.5957 + if q > 0.5226: + q = 0.5226 return (y, i, q) def yiq_to_rgb(y, i, q): @@ -94,6 +108,19 @@ def rgb_to_hls(r, g, b): else: h = 4.0+gc-rc h = (h/6.0) % 1.0 + + if h < 0.0: + h = 0.0 + if l < 0.0: + l = 0.0 + if s < 0.0: + s = 0.0 + if h > 1.0: + h = 1.0 + if l > 1.0: + l = 1.0 + if s > 1.0: + s = 1.0 return h, l, s def hls_to_rgb(h, l, s): @@ -104,7 +131,21 @@ def hls_to_rgb(h, l, s): else: m2 = l+s-(l*s) m1 = 2.0*l - m2 - return (_v(m1, m2, h+ONE_THIRD), _v(m1, m2, h), _v(m1, m2, h-ONE_THIRD)) + r, g, b = _v(m1, m2, h+ONE_THIRD), _v(m1, m2, h), _v(m1, m2, h-ONE_THIRD) + + if r < 0.0: + r = 0.0 + if g < 0.0: + g = 0.0 + if b < 0.0: + b = 0.0 + if r > 1.0: + r = 1.0 + if g > 1.0: + g = 1.0 + if b > 1.0: + b = 1.0 + return (r,g,b) def _v(m1, m2, hue): hue = hue % 1.0 @@ -140,6 +181,19 @@ def rgb_to_hsv(r, g, b): else: h = 4.0+gc-rc h = (h/6.0) % 1.0 + + if h < 0.0: + h = 0.0 + if s < 0.0: + s = 0.0 + if v < 0.0: + v = 0.0 + if h > 1.0: + h = 1.0 + if s > 1.0: + s = 1.0 + if v > 1.0: + v = 1.0 return h, s, v def hsv_to_rgb(h, s, v): @@ -151,6 +205,23 @@ def hsv_to_rgb(h, s, v): q = v*(1.0 - s*f) t = v*(1.0 - s*(1.0-f)) i = i%6 + + if v < 0.0: + v = 0.0 + if t < 0.0: + t = 0.0 + if p < 0.0: + p = 0.0 + if q < 0.0: + q = 0.0 + if v > 1.0: + v = 1.0 + if t > 1.0: + t = 1.0 + if p > 1.0: + p = 1.0 + if q > 1.0: + q = 1.0 if i == 0: return v, t, p if i == 1: From e8070bfa4c793250043c5942ffa748d5e19381b6 Mon Sep 17 00:00:00 2001 From: manoj Date: Fri, 8 May 2026 21:52:36 +0530 Subject: [PATCH 2/5] Add clipping for all conversion functions in colorsys to maintain validity and consistency --- Lib/colorsys.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/colorsys.py b/Lib/colorsys.py index b9616e66ed075e..db536e24186895 100644 --- a/Lib/colorsys.py +++ b/Lib/colorsys.py @@ -234,4 +234,4 @@ def hsv_to_rgb(h, s, v): return t, p, v if i == 5: return v, p, q - # Cannot get here + # Cannot get here \ No newline at end of file From 5770d116c1c03a5d7594eee06ab42943cd03bb4c Mon Sep 17 00:00:00 2001 From: manoj Date: Fri, 8 May 2026 22:06:26 +0530 Subject: [PATCH 3/5] Fix Lint Issue --- Lib/colorsys.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/colorsys.py b/Lib/colorsys.py index db536e24186895..b9616e66ed075e 100644 --- a/Lib/colorsys.py +++ b/Lib/colorsys.py @@ -234,4 +234,4 @@ def hsv_to_rgb(h, s, v): return t, p, v if i == 5: return v, p, q - # Cannot get here \ No newline at end of file + # Cannot get here From b62442d6da0b41f7f8871c8c80e075b8d4bc4f0a Mon Sep 17 00:00:00 2001 From: manoj Date: Fri, 8 May 2026 22:10:19 +0530 Subject: [PATCH 4/5] Fix Lint Issue by removing trailing whitespaces --- Lib/colorsys.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/colorsys.py b/Lib/colorsys.py index b9616e66ed075e..b61873927c31dd 100644 --- a/Lib/colorsys.py +++ b/Lib/colorsys.py @@ -181,7 +181,7 @@ def rgb_to_hsv(r, g, b): else: h = 4.0+gc-rc h = (h/6.0) % 1.0 - + if h < 0.0: h = 0.0 if s < 0.0: From 62c8b8c2d1b1e4086666155f2c01e8595e293aec Mon Sep 17 00:00:00 2001 From: manoj Date: Fri, 8 May 2026 22:13:23 +0530 Subject: [PATCH 5/5] Fix Lint Issue by removing trailing whitespaces in hls_to_rgb() --- Lib/colorsys.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/colorsys.py b/Lib/colorsys.py index b61873927c31dd..2195a5fb6a0b9f 100644 --- a/Lib/colorsys.py +++ b/Lib/colorsys.py @@ -132,7 +132,7 @@ def hls_to_rgb(h, l, s): m2 = l+s-(l*s) m1 = 2.0*l - m2 r, g, b = _v(m1, m2, h+ONE_THIRD), _v(m1, m2, h), _v(m1, m2, h-ONE_THIRD) - + if r < 0.0: r = 0.0 if g < 0.0: