Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
csscom
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Taddeüs Kroes
csscom
Commits
2737089c
Commit
2737089c
authored
Dec 31, 2012
by
Taddeüs Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added functions for converting colors
parent
e168aaf3
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
74 additions
and
0 deletions
+74
-0
color.py
color.py
+63
-0
tests/test_color.py
tests/test_color.py
+11
-0
No files found.
color.py
0 → 100644
View file @
2737089c
import
re
NAME_TO_HEX
=
{
'aqua'
:
'#0ff'
,
'black'
:
'#000'
,
'blue'
:
'#00f'
,
'fuchsia'
:
'#f0f'
,
'lime'
:
'#0f0'
,
'white'
:
'#fff'
,
'yellow'
:
'#ff0'
}
HEX_TO_NAME
=
{
'#808080'
:
'gray'
,
'#008000'
:
'green'
,
'#800000'
:
'maroon'
,
'#000080'
:
'navy'
,
'#8080000'
:
'olive'
,
'#800080'
:
'purple'
,
'#f00'
:
'red'
,
'#c0c0c0'
:
'silver'
,
'#008080'
:
'teal'
}
def
_rgb_to_hex
(
rgb
):
return
'#%02x%02x%02x'
%
rgb
def
color_shortcut
(
color
):
color
=
color
.
lower
()
# 'grey' and 'gray' are synonyms im most browsers, use 'gray' for correct
# behaviour in IE
if
color
==
'grey'
:
color
=
'gray'
# Try converting RGB to hexadecimal, which is always shorter
rgb
=
re
.
search
(
r'^rgb\
((
\d{1,3}), (\
d{
1,3}), (\
d{
1,3})\
)$
', color)
if rgb:
color = _rgb_to_hex(map(int, rgb.groups()))
# Check if hexadecimal code
hexa = re.search(r'
^
#([a-z0-9]{6})$', color)
if
hexa
:
code
=
hexa
.
group
(
1
)
# Check if a 3-character variant is possible, e.g. 11ff00 -> 1f0
if
code
[
0
]
==
code
[
1
]
and
code
[
2
]
==
code
[
3
]
and
code
[
4
]
==
code
[
5
]:
color
=
'#'
+
code
[
0
]
+
code
[
2
]
+
code
[
4
]
# Try to replace long hexadecimals with shorter color names
if
color
in
HEX_TO_NAME
:
return
HEX_TO_NAME
[
color
]
elif
color
in
NAME_TO_HEX
:
# Long color names can be replaced with shorted hexadecimal codes
return
NAME_TO_HEX
[
color
]
return
color
tests/test_color.py
0 → 100644
View file @
2737089c
from
unittest
import
TestCase
from
color
import
_rgb_to_hex
,
color_shortcut
class
TestColor
(
TestCase
):
def
test__rgb_to_hex
(
self
):
pass
def
test_color_shortcut
(
self
):
pass
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment