Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc0f997d

Browse files
committed
Micro-optimize _to_rgba_no_colorcycle.
This patch speeds up conversions of `#rgba`-type formats by between 25%and 40% (while shortening the implementation), although real benefitsshould be limited because of caching in to_rgba.
1 parent2f6589d commitc0f997d

File tree

1 file changed

+18
-29
lines changed

1 file changed

+18
-29
lines changed

‎lib/matplotlib/colors.py

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -372,40 +372,29 @@ def _to_rgba_no_colorcycle(c, alpha=None):
372372
# This may turn c into a non-string, so we check again below.
373373
c=_colors_full_map[c]
374374
exceptKeyError:
375-
iflen(orig_c)!=1:
375+
iflen(c)!=1:
376376
try:
377377
c=_colors_full_map[c.lower()]
378378
exceptKeyError:
379379
pass
380380
ifisinstance(c,str):
381-
# hex color in #rrggbb format.
382-
match=re.match(r"\A#[a-fA-F0-9]{6}\Z",c)
383-
ifmatch:
384-
return (tuple(int(n,16)/255
385-
fornin [c[1:3],c[3:5],c[5:7]])
386-
+ (alphaifalphaisnotNoneelse1.,))
387-
# hex color in #rgb format, shorthand for #rrggbb.
388-
match=re.match(r"\A#[a-fA-F0-9]{3}\Z",c)
389-
ifmatch:
390-
return (tuple(int(n,16)/255
391-
fornin [c[1]*2,c[2]*2,c[3]*2])
392-
+ (alphaifalphaisnotNoneelse1.,))
393-
# hex color with alpha in #rrggbbaa format.
394-
match=re.match(r"\A#[a-fA-F0-9]{8}\Z",c)
395-
ifmatch:
396-
color= [int(n,16)/255
397-
fornin [c[1:3],c[3:5],c[5:7],c[7:9]]]
398-
ifalphaisnotNone:
399-
color[-1]=alpha
400-
returntuple(color)
401-
# hex color with alpha in #rgba format, shorthand for #rrggbbaa.
402-
match=re.match(r"\A#[a-fA-F0-9]{4}\Z",c)
403-
ifmatch:
404-
color= [int(n,16)/255
405-
fornin [c[1]*2,c[2]*2,c[3]*2,c[4]*2]]
406-
ifalphaisnotNone:
407-
color[-1]=alpha
408-
returntuple(color)
381+
ifre.match(r"\A#[a-fA-F0-9]+\Z",c):
382+
iflen(c)==7:# #rrggbb hex format.
383+
return (*[n/0xffforninbytes.fromhex(c[1:])],
384+
alphaifalphaisnotNoneelse1.)
385+
eliflen(c)==4:# #rgb hex format, shorthand for #rrggbb.
386+
return (*[int(n,16)*0x11/0xffforninc[1:]],
387+
alphaifalphaisnotNoneelse1.)
388+
eliflen(c)==9:# #rrggbbaa hex format.
389+
color= [n/0xffforninbytes.fromhex(c[1:])]
390+
ifalphaisnotNone:
391+
color[-1]=alpha
392+
returntuple(color)
393+
eliflen(c)==5:# #rgba hex format, shorthand for #rrggbbaa.
394+
color= [int(n,16)*0x11/0xffforninc[1:]]
395+
ifalphaisnotNone:
396+
color[-1]=alpha
397+
returntuple(color)
409398
# string gray.
410399
try:
411400
c=float(c)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp