Skip to content

Commit

Permalink
New modifier to allow converting rgba value to argb
Browse files Browse the repository at this point in the history
  • Loading branch information
0x20F committed Jun 19, 2021
1 parent ac3bd02 commit b5f8bcd
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
13 changes: 10 additions & 3 deletions ix.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ def rgb(value, alpha = None):


@staticmethod
def hex(value, alpha = None):
def hex(value, alpha = None, argb = None):
'''
Take an rgb/rgba string and convert it to a hex representation
of the same color. If a hex string is provided, it'll return the exact
Expand All @@ -406,8 +406,12 @@ def hex(value, alpha = None):
# Give it back as it is if no overrides are specified
if not alpha: return value

value = value[0:7]
return f'{value}{alpha}'
value = value[1:7]

if argb:
return f'#{alpha}{value}'

return f'#{value}{alpha}'

a = value.startswith('rgba')
value = value.split('(', 1).pop().rstrip(')').split(',')
Expand All @@ -419,6 +423,9 @@ def hex(value, alpha = None):

if alpha: a = alpha

if argb:
return f'#{a}{r}{g}{b}'

return f'#{r}{g}{b}{a}'


Expand Down
6 changes: 6 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,12 @@ def test_helper_colors(self):
# hex rgba variable override
self.assertTrue('#f2f2f266' in parsed)

# hex rgb to argb
self.assertTrue('#66f2f2f2' in parsed)

# hex rgba to argb
self.assertTrue('#66000000' in parsed)


def test_helper_variable_format(self):
import ix
Expand Down
8 changes: 7 additions & 1 deletion tests/helpers_colors/main
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,10 @@ hex from rgba variable
#{{ hex [ colors.white-transparent ] }}

hex rgba variable override
#{{ hex [ colors.white-transparent ]; alpha: 0.4 }}
#{{ hex [ colors.white-transparent ]; alpha: 0.4 }}

hex rgb to argb
#{{ hex [ colors.white-transparent ]; argb: true; alpha: 0.4 }}

hex rgba to argb
#{{ hex [ colors.black-transparent ]; argb: true; alpha: 0.4 }}

0 comments on commit b5f8bcd

Please sign in to comment.