Skip to content
Snippets Groups Projects
Commit fc316fd2 authored by Robert Goldmann's avatar Robert Goldmann
Browse files

BaseUtils: added color helper + unit tests

parent f9f0126e
No related branches found
No related tags found
No related merge requests found
from __future__ import annotations
from dataclasses import dataclass
@dataclass
class Color:
red: int
green: int
blue: int
opacity: float = 1.0
@staticmethod
def from_hex(hexString: str) -> Color:
hexString = hexString.lstrip('#')
if len(hexString) == 3:
parts = [int(f'{hexString[i:i + 1]}{hexString[i:i + 1]}', 16) for i in range(0, 3)]
elif len(hexString) == 6:
parts = [int(hexString[i:i + 2], 16) for i in (0, 2, 4)]
elif len(hexString) == 8:
parts = [int(hexString[i:i + 2], 16) for i in (0, 2, 4, 6)]
else:
raise ValueError('Provided hex string has invalid format!')
return Color(parts[0], parts[1], parts[2], (parts[3] / 255.0) if len(parts) == 4 else 1.0)
def to_hex(self) -> str:
opacity = int(self.opacity * 255)
return '#{:02X}{:02X}{:02X}{:02X}'.format(self.red, self.green, self.blue, opacity)
def to_hex_without_opacity(self) -> str:
return '#{:02X}{:02X}{:02X}'.format(self.red, self.green, self.blue)
def to_rgba(self) -> str:
return f'rgba({self.red}, {self.green}, {self.blue}, {self.opacity})'
def to_rgb(self) -> str:
return f'rgb({self.red}, {self.green}, {self.blue})'
from TheCodeLabs_BaseUtils.DefaultLogger import DefaultLogger
from TheCodeLabs_BaseUtils.MailHandler import MailHandler
from TheCodeLabs_BaseUtils.CachedService import CachedService
import TheCodeLabs_BaseUtils.OverrideDecorator
import unittest
from TheCodeLabs_BaseUtils.TheCodeLabs_BaseUtils.Color import Color
class ColorTest(unittest.TestCase):
def test_to_hex(self):
color = Color(255, 0, 0)
self.assertEqual('#FF0000FF', color.to_hex())
def test_to_hex_without_opacity(self):
color = Color(255, 0, 0)
self.assertEqual('#FF0000', color.to_hex_without_opacity())
def test_to_rgba(self):
color = Color(255, 0, 0)
self.assertEqual('rgba(255, 0, 0, 1.0)', color.to_rgba())
def test_to_rgb(self):
color = Color(255, 0, 0)
self.assertEqual('rgb(255, 0, 0)', color.to_rgb())
def test_from_hex_normal(self):
hexString = '#FF0000'
color = Color.from_hex(hexString)
self.assertEqual(255, color.red)
self.assertEqual(0, color.green)
self.assertEqual(0, color.blue)
self.assertAlmostEqual(1.0, color.opacity, 2)
def test_from_hex_with_opacity(self):
hexString = '#FF000080'
color = Color.from_hex(hexString)
self.assertEqual(255, color.red)
self.assertEqual(0, color.green)
self.assertEqual(0, color.blue)
self.assertAlmostEqual(0.5, color.opacity, 2)
def test_from_hex_short(self):
hexString = '#F0F'
color = Color.from_hex(hexString)
self.assertEqual(255, color.red)
self.assertEqual(0, color.green)
self.assertEqual(255, color.blue)
self.assertAlmostEqual(1.0, color.opacity, 2)
def test_from_invalid(self):
hexString = '#12345'
with self.assertRaises(ValueError):
Color.from_hex(hexString)
if __name__ == '__main__':
unittest.main()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment