From 99379a9835195760b4b7b35eb21773a24eccff18 Mon Sep 17 00:00:00 2001 From: Marek Mazij <112333347+Mrk-Mzj@users.noreply.github.com> Date: Sat, 21 Oct 2023 02:30:08 +0200 Subject: [PATCH 1/3] feat: code functional, commented, tested --- conversions/rgb_cmyk_conversion.py | 68 ++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 conversions/rgb_cmyk_conversion.py diff --git a/conversions/rgb_cmyk_conversion.py b/conversions/rgb_cmyk_conversion.py new file mode 100644 index 000000000000..acecf41f757b --- /dev/null +++ b/conversions/rgb_cmyk_conversion.py @@ -0,0 +1,68 @@ +def rgb_to_cmyk(r_input, g_input, b_input: int) -> tuple[int, int, int, int]: + """ + Simple RGB to CMYK conversion. Returns percentages of CMYK paint. + https://www.programmingalgorithms.com/algorithm/rgb-to-cmyk/ + + Note: this is a very popular algorithm that converts colors linearly and gives + only approximate results. Actual preparation for printing requires advanced color + conversion considering the color profiles and parameters of the target device. + + >>> rgb_to_cmyk(255, 200, "a") + Traceback (most recent call last): + ... + ValueError: Expected int as input, found (, , ) + + >>> rgb_to_cmyk(255, 255, 999) + Traceback (most recent call last): + ... + ValueError: Expected int of the range 0..255 + + >>> rgb_to_cmyk(255, 255, 255) # white + (0, 0, 0, 0) + + >>> rgb_to_cmyk(128, 128, 128) # gray + (0, 0, 0, 50) + + >>> rgb_to_cmyk(0, 0, 0) # black + (0, 0, 0, 100) + + >>> rgb_to_cmyk(255, 0, 0) # red + (0, 100, 100, 0) + + >>> rgb_to_cmyk(0, 255, 0) # green + (100, 0, 100, 0) + + >>> rgb_to_cmyk(0, 0, 255) # blue + (100, 100, 0, 0) + """ + + if type(r_input) != int or type(g_input) != int or type(b_input) != int: + raise ValueError( + f"Expected int as input, found {type(r_input), type(g_input), type(b_input)}" + ) + + if not 0 <= r_input < 256 or not 0 <= g_input < 256 or not 0 <= b_input < 256: + raise ValueError("Expected int of the range 0..255") + + # changing range from 0..255 to 0..1 + r = r_input / 255 + g = g_input / 255 + b = b_input / 255 + + k = 1 - max(r, g, b) + + if k == 1: # pure black + return 0, 0, 0, 100 + + c = round(100 * (1 - r - k) / (1 - k)) + m = round(100 * (1 - g - k) / (1 - k)) + y = round(100 * (1 - b - k) / (1 - k)) + k = round(100 * k) + + return c, m, y, k + + +if __name__ == "__main__": + from doctest import testmod + + testmod() From 75979fb7c2d04b3e9d675eb6aa889a9fac8c0be9 Mon Sep 17 00:00:00 2001 From: Marek Mazij <112333347+Mrk-Mzj@users.noreply.github.com> Date: Sat, 21 Oct 2023 03:08:26 +0200 Subject: [PATCH 2/3] fix: compering types, exception msg, line length --- conversions/rgb_cmyk_conversion.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/conversions/rgb_cmyk_conversion.py b/conversions/rgb_cmyk_conversion.py index acecf41f757b..740f3db1b4ab 100644 --- a/conversions/rgb_cmyk_conversion.py +++ b/conversions/rgb_cmyk_conversion.py @@ -10,7 +10,7 @@ def rgb_to_cmyk(r_input, g_input, b_input: int) -> tuple[int, int, int, int]: >>> rgb_to_cmyk(255, 200, "a") Traceback (most recent call last): ... - ValueError: Expected int as input, found (, , ) + ValueError: Expected int, found (, , ) >>> rgb_to_cmyk(255, 255, 999) Traceback (most recent call last): @@ -36,10 +36,13 @@ def rgb_to_cmyk(r_input, g_input, b_input: int) -> tuple[int, int, int, int]: (100, 100, 0, 0) """ - if type(r_input) != int or type(g_input) != int or type(b_input) != int: - raise ValueError( - f"Expected int as input, found {type(r_input), type(g_input), type(b_input)}" - ) + if ( + not isinstance(r_input, int) + or not isinstance(g_input, int) + or not isinstance(b_input, int) + ): + msg = f"Expected int, found {type(r_input), type(g_input), type(b_input)}" + raise ValueError(msg) if not 0 <= r_input < 256 or not 0 <= g_input < 256 or not 0 <= b_input < 256: raise ValueError("Expected int of the range 0..255") From 063e2c45364f2d470ce940c13135f8715fe380af Mon Sep 17 00:00:00 2001 From: Marek Mazij <112333347+Mrk-Mzj@users.noreply.github.com> Date: Sat, 21 Oct 2023 03:13:38 +0200 Subject: [PATCH 3/3] fix: type hints --- conversions/rgb_cmyk_conversion.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conversions/rgb_cmyk_conversion.py b/conversions/rgb_cmyk_conversion.py index 740f3db1b4ab..07d65b704c44 100644 --- a/conversions/rgb_cmyk_conversion.py +++ b/conversions/rgb_cmyk_conversion.py @@ -1,4 +1,4 @@ -def rgb_to_cmyk(r_input, g_input, b_input: int) -> tuple[int, int, int, int]: +def rgb_to_cmyk(r_input: int, g_input: int, b_input: int) -> tuple[int, int, int, int]: """ Simple RGB to CMYK conversion. Returns percentages of CMYK paint. https://www.programmingalgorithms.com/algorithm/rgb-to-cmyk/