Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hex value option to color methods #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 61 additions & 7 deletions phoebusgen/widget/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,17 +237,44 @@ def predefined_foreground_color(self, name: object) -> None:
e = self._shared.create_element(self.root, 'foreground_color')
self._shared.create_color_element(e, name, None, None, None, None)

def foreground_color(self, red: int, green: int, blue: int, alpha: int = 255) -> None:
def foreground_color(self, *args) -> None:
"""
Add foreground color property to widget with RGB values
Add foreground color property to widget with RGB values or HEX value

:param red: 0-255
:param green: 0-255
:param blue: 0-255
:param alpha: 0-255. Default is 255

or

:param hex: #000000-#FFFFFF
:param alpha: 0-255. Default is 255
"""
e = self._shared.create_element(self.root, 'foreground_color')
self._shared.create_color_element(e, None, red, green, blue, alpha)
alpha = 255
if len(args) == 4 and isinstance(args[0], int) and isinstance(args[1], int) and isinstance(args[2], int) and isinstance(args[3], int):
red = args[0]
green = args[1]
blue = args[2]
alpha = args[3]
self._shared.create_color_element(e, None, red, green, blue, alpha)
elif len(args) == 3 and isinstance(args[0], int) and isinstance(args[1], int) and isinstance(args[2], int):
red = args[0]
green = args[1]
blue = args[2]
self._shared.create_color_element(e, None, red, green, blue, alpha)
elif len(args) == 2 and isinstance(args[0], str) and isinstance(args[1], int):
hex = args[0].lstrip('#')
alpha = args[1]
rgb = tuple(int(hex[i:i+2], 16) for i in (0, 2, 4))
self._shared.create_color_element(e, None, rgb[0], rgb[1], rgb[2], alpha)
elif len(args) == 1 and isinstance(args[0], str):
hex = args[0].lstrip('#')
rgb = tuple(int(hex[i:i+2], 16) for i in (0, 2, 4))
self._shared.create_color_element(e, None, rgb[0], rgb[1], rgb[2], alpha)
else:
raise TypeError('Parameters should be (red: int, green: int, blue: int, alpha: int) or (hex: str, alpha: int)')

class _BackgroundColor(object):
def predefined_background_color(self, name: object) -> None:
Expand All @@ -258,17 +285,44 @@ def predefined_background_color(self, name: object) -> None:
e = self._shared.create_element(self.root, 'background_color')
self._shared.create_color_element(e, name, None, None, None, None)

def background_color(self, red: int, green: int, blue: int, alpha: int = 255) -> None:
def background_color(self, *args) -> None:
"""
Add background color property to widget with RGB values
Add background color property to widget with RGB values or HEX value

:param red: 0-255
:param green: 0-255
:param blue: 0-255
:param alpha: 0-255. Default is 255

or

:param hex: #000000-#FFFFFF
:param alpha: 0-255. Default is 255
"""
e = self._shared.create_element(self.root, 'background_color')
self._shared.create_color_element(e, None, red, green, blue, alpha)
alpha = 255
if len(args) == 4 and isinstance(args[0], int) and isinstance(args[1], int) and isinstance(args[2], int) and isinstance(args[3], int):
red = args[0]
green = args[1]
blue = args[2]
alpha = args[3]
self._shared.create_color_element(e, None, red, green, blue, alpha)
elif len(args) == 3 and isinstance(args[0], int) and isinstance(args[1], int) and isinstance(args[2], int):
red = args[0]
green = args[1]
blue = args[2]
self._shared.create_color_element(e, None, red, green, blue, alpha)
elif len(args) == 2 and isinstance(args[0], str) and isinstance(args[1], int):
hex = args[0].lstrip('#')
alpha = args[1]
rgb = tuple(int(hex[i:i+2], 16) for i in (0, 2, 4))
self._shared.create_color_element(e, None, rgb[0], rgb[1], rgb[2], alpha)
elif len(args) == 1 and isinstance(args[0], str):
hex = args[0].lstrip('#')
rgb = tuple(int(hex[i:i+2], 16) for i in (0, 2, 4))
self._shared.create_color_element(e, None, rgb[0], rgb[1], rgb[2], alpha)
else:
raise TypeError('Parameters should be (red: int, green: int, blue: int, alpha: int) or (hex: str, alpha: int)')

class _Transparent(object):
def transparent(self, transparent: bool = False) -> None:
Expand Down Expand Up @@ -758,7 +812,7 @@ def action_execute_as_one(self, val: bool) -> None:
# target should be an enum
def action_open_display(self, file: str, target: str, description: str = None, macros: dict = None) -> None:
"""
Add open display action to widget. description and macros are optional params
Add open display action to widget. description and macros are optional args

:param file: File name to open
:param target: <specific strings only> tab, replace, window
Expand Down
47 changes: 45 additions & 2 deletions tests/property_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,11 +409,33 @@ def test_predefined_foreground_color(self):
self.child_element_test(tag_name, 'color', None, {'name': 'Background', 'red': '255', 'green': '255',
'blue': '255', 'alpha': '255'})

def test_foreground_color(self):
def test_foreground_color_rgb(self):
tag_name = 'foreground_color'
self.element.foreground_color(5, 10, 15)
self.child_element_test(tag_name, 'color', None, {'red': '5', 'green': '10',
'blue': '15', 'alpha': '255'})

def test_foreground_color_rgb_alpha(self):
tag_name = 'foreground_color'
self.element.foreground_color(5, 10, 15, 100)
self.child_element_test(tag_name, 'color', None, {'red': '5', 'green': '10',
'blue': '15', 'alpha': '100'})

def test_foreground_color_hex(self):
tag_name = 'foreground_color'
self.element.foreground_color('#ffffc3')
self.child_element_test(tag_name, 'color', None, {'red': '255', 'green': '255',
'blue': '195', 'alpha': '255'})

def test_foreground_color_hex_alpha(self):
tag_name = 'foreground_color'
self.element.foreground_color('#ffffc3', 100)
self.child_element_test(tag_name, 'color', None, {'red': '255', 'green': '255',
'blue': '195', 'alpha': '100'})

def test_foreground_color_bad_args(self):
tag_name = 'foreground_color'
self.assertRaises(TypeError, self.element.foreground_color, 5, 10, 15, '#ffffff')


class TestBackgroundColor(GenericTest):
Expand All @@ -422,10 +444,31 @@ def test_predefined_background_color(self):
self.element.predefined_background_color(self.colors.MINOR)
self.child_element_test(tag_name, 'color', None, {'name': 'MINOR', 'red': '255', 'green': '128', 'blue': '0', 'alpha': '255'})

def test_background_color(self):
def test_background_color_rgb(self):
tag_name = 'background_color'
self.element.background_color(5, 10, 15)
self.child_element_test(tag_name, 'color', None, {'red': '5', 'green': '10', 'blue': '15', 'alpha': '255'})

def test_background_color_rgb_alpha(self):
tag_name = 'background_color'
self.element.background_color(5, 10, 15, 100)
self.child_element_test(tag_name, 'color', None, {'red': '5', 'green': '10', 'blue': '15', 'alpha': '100'})

def test_background_color_hex(self):
tag_name = 'background_color'
self.element.background_color('#ffffc3')
self.child_element_test(tag_name, 'color', None, {'red': '255', 'green': '255',
'blue': '195', 'alpha': '255'})

def test_background_color_hex_alpha(self):
tag_name = 'background_color'
self.element.background_color('#ffffc3', 100)
self.child_element_test(tag_name, 'color', None, {'red': '255', 'green': '255',
'blue': '195', 'alpha': '100'})

def test_background_color_bad_args(self):
tag_name = 'background_color'
self.assertRaises(TypeError, self.element.background_color, 5, 10, 15, '#ffffff')


class TestTransparent(GenericTest):
Expand Down