Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
csscom
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
Taddeüs Kroes
csscom
Commits
2737089c
Commit
2737089c
authored
12 years ago
by
Taddeüs Kroes
Browse files
Options
Downloads
Patches
Plain Diff
Added functions for converting colors
parent
e168aaf3
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
color.py
+63
-0
63 additions, 0 deletions
color.py
tests/test_color.py
+11
-0
11 additions, 0 deletions
tests/test_color.py
with
74 additions
and
0 deletions
color.py
0 → 100644
+
63
−
0
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
This diff is collapsed.
Click to expand it.
tests/test_color.py
0 → 100644
+
11
−
0
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
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment