Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
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
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
096c1a0f
Commit
096c1a0f
authored
Feb 11, 1998
by
Barry Warsaw
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Simplification of conversion routines
parent
698bd943
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
21 additions
and
22 deletions
+21
-22
Tools/pynche/ColorDB.py
Tools/pynche/ColorDB.py
+21
-22
No files found.
Tools/pynche/ColorDB.py
View file @
096c1a0f
...
...
@@ -15,10 +15,13 @@ Supporte file types are:
import
sys
import
re
from
types
import
*
class
BadColor
(
Exception
):
pass
DEFAULT_DB
=
None
# generic class
class
ColorDB
:
...
...
@@ -80,10 +83,11 @@ class ColorDB:
except
KeyError
:
raise
BadColor
(
name
)
def
nearest
(
self
,
r
ed
,
green
,
blu
e
):
def
nearest
(
self
,
r
gbtupl
e
):
# TBD: use Voronoi diagrams, Delaunay triangulation, or octree for
# speeding up the locating of nearest point. This is really
# inefficient!
red
,
green
,
blue
=
rgbtuple
nearest
=
-
1
nearest_name
=
''
for
name
,
aliases
in
self
.
__byrrggbb
.
values
():
...
...
@@ -132,6 +136,9 @@ def get_colordb(file, filetype=X_RGB_TXT):
finally
:
if
fp
:
fp
.
close
()
# save a global copy
global
DEFAULT_DB
DEFAULT_DB
=
colordb
return
colordb
...
...
@@ -141,28 +148,20 @@ def rrggbb_to_triplet(color):
if
color
[
0
]
<>
'#'
:
raise
BadColor
(
color
)
zero
=
ord
(
'0'
)
a
=
ord
(
'a'
)
A
=
ord
(
'A'
)
def
_hexchar
(
c
,
zero
=
zero
,
a
=
a
,
A
=
A
):
v
=
ord
(
c
)
if
v
>=
zero
and
v
<=
zero
+
9
:
return
v
-
zero
elif
v
>=
a
and
v
<=
a
+
26
:
return
v
-
a
+
10
elif
v
>=
A
and
v
<=
A
+
26
:
return
v
-
A
+
10
else
:
raise
BadColor
red
=
color
[
1
:
3
]
green
=
color
[
3
:
5
]
blue
=
color
[
5
:
7
]
return
tuple
(
map
(
lambda
v
:
string
.
atoi
(
v
,
16
),
(
red
,
green
,
blue
)))
try
:
digits
=
map
(
_hexchar
,
color
[
1
:])
except
BadColor
:
raise
BadColor
(
color
)
red
=
digits
[
0
]
*
16
+
digits
[
1
]
green
=
digits
[
2
]
*
16
+
digits
[
3
]
blue
=
digits
[
4
]
*
16
+
digits
[
5
]
return
(
red
,
green
,
blue
)
def
triplet_to_rrggbb
(
rgbtuple
):
"""Converts a (red, green, blue) tuple to #rrggbb."""
def
hexify
(
v
):
hexstr
=
hex
(
v
)[
2
:
4
]
if
len
(
hexstr
)
<
2
:
hexstr
=
'0'
+
hexstr
return
hexstr
return
'#%s%s%s'
%
tuple
(
map
(
hexify
,
rgbtuple
))
...
...
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