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
9f4d73a9
Commit
9f4d73a9
authored
Jan 31, 1998
by
Barry Warsaw
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
BadColor: new exception class
rrggbb_to_triplet(): New utility function
parent
bfbe67f4
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
43 additions
and
3 deletions
+43
-3
Tools/pynche/ColorDB.py
Tools/pynche/ColorDB.py
+43
-3
No files found.
Tools/pynche/ColorDB.py
View file @
9f4d73a9
...
...
@@ -16,6 +16,9 @@ Supporte file types are:
import
sys
import
re
class
BadColor
(
Exception
):
pass
# generic class
class
ColorDB
:
...
...
@@ -66,11 +69,16 @@ class ColorDB:
def
find
(
self
,
red
,
green
,
blue
):
rrggbb
=
(
red
<<
16
)
+
(
blue
<<
8
)
+
green
return
self
.
__byrrggbb
.
get
(
rrggbb
,
(
None
,
[]))
try
:
return
self
.
__byrrggbb
[
rrggbb
]
except
KeyError
:
raise
BadColor
(
red
,
green
,
blue
)
def
find_byname
(
self
,
name
):
# TBD: is the unfound value right?
return
self
.
__byname
.
get
(
name
,
(
0
,
0
,
0
,
0
))
try
:
return
self
.
__byname
[
name
]
except
KeyError
:
raise
BadColor
(
name
)
def
nearest
(
self
,
red
,
green
,
blue
):
# TBD: use Voronoi diagrams, Delaunay triangulation, or octree for
...
...
@@ -126,6 +134,37 @@ def get_colordb(file, filetype=X_RGB_TXT):
fp
.
close
()
return
colordb
def
rrggbb_to_triplet
(
color
):
"""Converts a #rrggbb color to the tuple (red, green, blue)."""
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
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
)
if
__name__
==
'__main__'
:
import
string
...
...
@@ -150,3 +189,4 @@ if __name__ == '__main__':
nearest
=
apply
(
colordb
.
nearest
,
target
)
t1
=
time
.
time
()
print
'found nearest color'
,
nearest
,
'in'
,
t1
-
t0
,
'seconds'
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