[Python-checkins] Improve consistency of colorsys.rgb_to_hsv (GH-27277)

nanjekyejoannah webhook-mailer at python.org
Fri Jul 23 08:59:34 EDT 2021


https://github.com/python/cpython/commit/7d28a6eb90e2e381e13ba21073c6868fdf6eb058
commit: 7d28a6eb90e2e381e13ba21073c6868fdf6eb058
branch: main
author: seb-hub <45536464+seb-hub at users.noreply.github.com>
committer: nanjekyejoannah <33177550+nanjekyejoannah at users.noreply.github.com>
date: 2021-07-23T09:59:30-03:00
summary:

Improve consistency of colorsys.rgb_to_hsv (GH-27277)

Cache repeated difference to make code easier to read and consistent with colorsys.rgb_to_hls.

files:
M Lib/colorsys.py

diff --git a/Lib/colorsys.py b/Lib/colorsys.py
index 0f52512a67d87..9bdc83e377260 100644
--- a/Lib/colorsys.py
+++ b/Lib/colorsys.py
@@ -125,13 +125,14 @@ def _v(m1, m2, hue):
 def rgb_to_hsv(r, g, b):
     maxc = max(r, g, b)
     minc = min(r, g, b)
+    rangec = (maxc-minc)
     v = maxc
     if minc == maxc:
         return 0.0, 0.0, v
-    s = (maxc-minc) / maxc
-    rc = (maxc-r) / (maxc-minc)
-    gc = (maxc-g) / (maxc-minc)
-    bc = (maxc-b) / (maxc-minc)
+    s = rangec / maxc
+    rc = (maxc-r) / rangec
+    gc = (maxc-g) / rangec
+    bc = (maxc-b) / rangec
     if r == maxc:
         h = bc-gc
     elif g == maxc:



More information about the Python-checkins mailing list