From e19ceae6e16740f5b885e5af92d55909f76e83d7 Mon Sep 17 00:00:00 2001 From: CodeNinja-tech Date: Sat, 5 Nov 2022 22:33:02 +0100 Subject: [PATCH] =?UTF-8?q?`=F0=9F=8C=B1=20Finished:`=20Completed=20Projec?= =?UTF-8?q?t?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- convert_rgb_to_hex.py | 48 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 convert_rgb_to_hex.py diff --git a/convert_rgb_to_hex.py b/convert_rgb_to_hex.py new file mode 100644 index 0000000..24a3aab --- /dev/null +++ b/convert_rgb_to_hex.py @@ -0,0 +1,48 @@ +def rgb_to_hex(rgb: str, out_format: str = 'native'): + # Convert an rgb string sequence to a hex value + # The format for the data is r,g,b so these will be in an array latter + + # Convert the string into an array + rgb_arr: [] = rgb.replace(' ', '').split(',') + + # Check if it's length is not anything else but three + if len(rgb_arr) == 3 and not rgb_arr.__contains__(''): # Check for empty strings too + # Construct a HEX code from the loop + + # Is the string valid? + is_valid_string: bool = False + + # Check the format for the output defaults to 'native' + if out_format == 'native': + hex_value = '0x' + elif out_format == 'color': + hex_value = '#' + else: + hex_value = '' + + # Loop through the items + for value in rgb_arr: + # Convert the str into an int value + int_value = int(value) + + if 0 <= int_value <= 255: + hex_value += '%x' % int_value + is_valid_string = True + else: + print('That\'s not a valid RGB color') + is_valid_string = False + break + + # Print no answer if the string is not valid + if is_valid_string: + if out_format == 'color': + print(f'Your Input: {rgb} -> HEX Code: {str(hex_value).upper()} (color mode)') + else: + print(f'Your Input: {rgb} -> HEX Code: {hex_value} (native mode)') + + else: + print('That isn\'t a valid input') + + +# Try it out -> '255, 255, 255', 'color' +rgb_to_hex(str(input('Enter a valid RGB string (comma separated): ')), str(input('Formatting Mode (color/ native): ')))