Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
5c458052
Kaydet (Commit)
5c458052
authored
Eki 21, 2002
tarafından
Barry Warsaw
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
XConsortium. Now we can recognize these files!
üst
d9e0e1f4
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
68 additions
and
67 deletions
+68
-67
ColorDB.py
Tools/pynche/ColorDB.py
+68
-67
No files found.
Tools/pynche/ColorDB.py
Dosyayı görüntüle @
5c458052
...
...
@@ -39,42 +39,42 @@ class ColorDB:
def
__init__
(
self
,
fp
):
lineno
=
2
self
.
__name
=
fp
.
name
# Maintain several dictionaries for indexing into the color database.
# Note that while Tk supports RGB intensities of 4, 8, 12, or 16 bits,
# for now we only support 8 bit intensities. At least on OpenWindows,
# all intensities in the /usr/openwin/lib/rgb.txt file are 8-bit
#
# key is (red, green, blue) tuple, value is (name, [aliases])
self
.
__byrgb
=
{}
# key is name, value is (red, green, blue)
self
.
__byname
=
{}
# Maintain several dictionaries for indexing into the color database.
# Note that while Tk supports RGB intensities of 4, 8, 12, or 16 bits,
# for now we only support 8 bit intensities. At least on OpenWindows,
# all intensities in the /usr/openwin/lib/rgb.txt file are 8-bit
#
# key is (red, green, blue) tuple, value is (name, [aliases])
self
.
__byrgb
=
{}
# key is name, value is (red, green, blue)
self
.
__byname
=
{}
# all unique names (non-aliases). built-on demand
self
.
__allnames
=
None
while
1
:
line
=
fp
.
readline
()
if
not
line
:
break
# get this compiled regular expression from derived class
mo
=
self
.
_re
.
match
(
line
)
if
not
mo
:
while
1
:
line
=
fp
.
readline
()
if
not
line
:
break
# get this compiled regular expression from derived class
mo
=
self
.
_re
.
match
(
line
)
if
not
mo
:
print
>>
sys
.
stderr
,
'Error in'
,
fp
.
name
,
' line'
,
lineno
lineno
+=
1
continue
# extract the red, green, blue, and name
lineno
+=
1
continue
# extract the red, green, blue, and name
red
,
green
,
blue
=
self
.
_extractrgb
(
mo
)
name
=
self
.
_extractname
(
mo
)
keyname
=
name
.
lower
()
# BAW: for now the `name' is just the first named color with the
# rgb values we find. Later, we might want to make the two word
# version the `name', or the CapitalizedVersion, etc.
key
=
(
red
,
green
,
blue
)
foundname
,
aliases
=
self
.
__byrgb
.
get
(
key
,
(
name
,
[]))
if
foundname
<>
name
and
foundname
not
in
aliases
:
aliases
.
append
(
name
)
self
.
__byrgb
[
key
]
=
(
foundname
,
aliases
)
# add to byname lookup
self
.
__byname
[
keyname
]
=
key
lineno
=
lineno
+
1
keyname
=
name
.
lower
()
# BAW: for now the `name' is just the first named color with the
# rgb values we find. Later, we might want to make the two word
# version the `name', or the CapitalizedVersion, etc.
key
=
(
red
,
green
,
blue
)
foundname
,
aliases
=
self
.
__byrgb
.
get
(
key
,
(
name
,
[]))
if
foundname
<>
name
and
foundname
not
in
aliases
:
aliases
.
append
(
name
)
self
.
__byrgb
[
key
]
=
(
foundname
,
aliases
)
# add to byname lookup
self
.
__byname
[
keyname
]
=
key
lineno
=
lineno
+
1
# override in derived classes
def
_extractrgb
(
self
,
mo
):
...
...
@@ -88,36 +88,36 @@ class ColorDB:
def
find_byrgb
(
self
,
rgbtuple
):
"""Return name for rgbtuple"""
try
:
return
self
.
__byrgb
[
rgbtuple
]
except
KeyError
:
raise
BadColor
(
rgbtuple
)
try
:
return
self
.
__byrgb
[
rgbtuple
]
except
KeyError
:
raise
BadColor
(
rgbtuple
)
def
find_byname
(
self
,
name
):
"""Return (red, green, blue) for name"""
name
=
name
.
lower
()
try
:
return
self
.
__byname
[
name
]
except
KeyError
:
raise
BadColor
(
name
)
name
=
name
.
lower
()
try
:
return
self
.
__byname
[
name
]
except
KeyError
:
raise
BadColor
(
name
)
def
nearest
(
self
,
red
,
green
,
blue
):
"""Return the name of color nearest (red, green, blue)"""
# BAW: should we use Voronoi diagrams, Delaunay triangulation, or
# octree for speeding up the locating of nearest point? Exhaustive
# search is inefficient, but seems fast enough.
nearest
=
-
1
nearest_name
=
''
for
name
,
aliases
in
self
.
__byrgb
.
values
():
r
,
g
,
b
=
self
.
__byname
[
name
.
lower
()]
rdelta
=
red
-
r
gdelta
=
green
-
g
bdelta
=
blue
-
b
distance
=
rdelta
*
rdelta
+
gdelta
*
gdelta
+
bdelta
*
bdelta
if
nearest
==
-
1
or
distance
<
nearest
:
nearest
=
distance
nearest_name
=
name
return
nearest_name
# BAW: should we use Voronoi diagrams, Delaunay triangulation, or
# octree for speeding up the locating of nearest point? Exhaustive
# search is inefficient, but seems fast enough.
nearest
=
-
1
nearest_name
=
''
for
name
,
aliases
in
self
.
__byrgb
.
values
():
r
,
g
,
b
=
self
.
__byname
[
name
.
lower
()]
rdelta
=
red
-
r
gdelta
=
green
-
g
bdelta
=
blue
-
b
distance
=
rdelta
*
rdelta
+
gdelta
*
gdelta
+
bdelta
*
bdelta
if
nearest
==
-
1
or
distance
<
nearest
:
nearest
=
distance
nearest_name
=
name
return
nearest_name
def
unique_names
(
self
):
# sorted
...
...
@@ -137,7 +137,7 @@ class ColorDB:
except
KeyError
:
raise
BadColor
((
red
,
green
,
blue
))
return
[
name
]
+
aliases
class
RGBColorDB
(
ColorDB
):
_re
=
re
.
compile
(
...
...
@@ -172,6 +172,7 @@ class WebsafeDB(ColorDB):
# the class to instantiate if a match is found
FILETYPES
=
[
(
re
.
compile
(
'Xorg'
),
RGBColorDB
),
(
re
.
compile
(
'XConsortium'
),
RGBColorDB
),
(
re
.
compile
(
'HTML'
),
HTML40DB
),
(
re
.
compile
(
'lightlink'
),
LightlinkDB
),
...
...
@@ -216,11 +217,11 @@ def rrggbb_to_triplet(color):
if
rgbtuple
is
None
:
if
color
[
0
]
<>
'#'
:
raise
BadColor
(
color
)
red
=
color
[
1
:
3
]
green
=
color
[
3
:
5
]
blue
=
color
[
5
:
7
]
red
=
color
[
1
:
3
]
green
=
color
[
3
:
5
]
blue
=
color
[
5
:
7
]
rgbtuple
=
int
(
red
,
16
),
int
(
green
,
16
),
int
(
blue
,
16
)
_namedict
[
color
]
=
rgbtuple
_namedict
[
color
]
=
rgbtuple
return
rgbtuple
...
...
@@ -230,8 +231,8 @@ def triplet_to_rrggbb(rgbtuple):
global
_tripdict
hexname
=
_tripdict
.
get
(
rgbtuple
)
if
hexname
is
None
:
hexname
=
'#
%02
x
%02
x
%02
x'
%
rgbtuple
_tripdict
[
rgbtuple
]
=
hexname
hexname
=
'#
%02
x
%02
x
%02
x'
%
rgbtuple
_tripdict
[
rgbtuple
]
=
hexname
return
hexname
...
...
@@ -253,17 +254,17 @@ def triplet_to_brightness(rgbtuple):
if
__name__
==
'__main__'
:
colordb
=
get_colordb
(
'/usr/openwin/lib/rgb.txt'
)
if
not
colordb
:
print
'No parseable color database found'
sys
.
exit
(
1
)
print
'No parseable color database found'
sys
.
exit
(
1
)
# on my system, this color matches exactly
target
=
'navy'
red
,
green
,
blue
=
rgbtuple
=
colordb
.
find_byname
(
target
)
print
target
,
':'
,
red
,
green
,
blue
,
triplet_to_rrggbb
(
rgbtuple
)
name
,
aliases
=
colordb
.
find_byrgb
(
rgbtuple
)
print
'name:'
,
name
,
'aliases:'
,
COMMASPACE
.
join
(
aliases
)
r
,
g
,
b
=
(
1
,
1
,
128
)
# nearest to navy
r
,
g
,
b
=
(
145
,
238
,
144
)
# nearest to lightgreen
r
,
g
,
b
=
(
255
,
251
,
250
)
# snow
r
,
g
,
b
=
(
1
,
1
,
128
)
# nearest to navy
r
,
g
,
b
=
(
145
,
238
,
144
)
# nearest to lightgreen
r
,
g
,
b
=
(
255
,
251
,
250
)
# snow
print
'finding nearest to'
,
target
,
'...'
import
time
t0
=
time
.
time
()
...
...
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