Skip to content

Commit

Permalink
updates function and tests file
Browse files Browse the repository at this point in the history
  • Loading branch information
joeywwwu committed Jan 18, 2024
1 parent f3c0abf commit 05c0a68
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/hexdropper/rgb_to_hex.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,18 @@ def rgb_to_hex(r, g, b):
>>> rgb_to_hex(0, 0, 255)
'0000FF' # Blue color
"""
return None
# Helper function to convert a single color value
def convert_color(color):
quotient = color // 16
remainder = int((color / 16 - quotient) * 16)
return quotient, remainder

# Convert each color and concatenate
rx, ry = convert_color(r)
gx, gy = convert_color(g)
bx, by = convert_color(b)

# Format string to ensure two characters for each color component
return "#{:X}{:X}{:X}{:X}{:X}{:X}".format(rx, ry, gx, gy, bx, by)


30 changes: 30 additions & 0 deletions tests/test_rgb_to_hex.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import sys
import os
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
from src.hexdropper.rgb_to_hex import rgb_to_hex

def test_standard_colors():
assert rgb_to_hex(255, 0, 0) == '#FF0000'
assert rgb_to_hex(0, 255, 0) == '#00FF00'
assert rgb_to_hex(0, 0, 255) == '#0000FF'

def test_boundary_values():
assert rgb_to_hex(0, 0, 0) == '#000000'
assert rgb_to_hex(255, 255, 255) == '#FFFFFF'

def test_specific_case():
assert rgb_to_hex(76, 150, 29) == '#4C961D'

def test_edge_cases():
assert rgb_to_hex(15, 15, 15) == '#0F0F0F'
assert rgb_to_hex(16, 16, 16) == '#101010'

def run_tests():
test_standard_colors()
test_boundary_values()
test_specific_case()
test_edge_cases()
print("All tests passed!")

if __name__ == "__main__":
run_tests()

0 comments on commit 05c0a68

Please sign in to comment.