Skip to content

Commit 0ffc1d1

Browse files
committed
Permit comments at head of color files
1 parent e85e94b commit 0ffc1d1

4 files changed

Lines changed: 24 additions & 9 deletions

File tree

proplot/colors/crayola.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# Crayola crayon colors
2+
# https://en.wikipedia.org/wiki/List_of_Crayola_crayon_colors
13
almond: #efdecd
24
antique brass: #cd9575
35
apricot: #fdd9b5

proplot/colors/opencolor.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# Open-color colors
2+
# https://yeun.github.io/open-color/
13
gray0: #f8f9fa
24
gray1: #f1f3f5
35
gray2: #e9ecef

proplot/colors/xkcd.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# XKCD color survey colors
2+
# https://blog.xkcd.com/2010/05/03/color-survey-results/
13
purple: #7e1e9c
24
green: #15b01a
35
blue: #0343df

proplot/styletools.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3047,16 +3047,25 @@ def register_colors(nmax=np.inf):
30473047
for file in paths:
30483048
cat, _ = os.path.splitext(os.path.basename(file))
30493049
with open(file, 'r') as f:
3050-
pairs = [
3051-
tuple(item.strip() for item in line.split(':'))
3052-
for line in f.readlines()
3053-
if line.strip() and line.strip()[0] != '#'
3054-
]
3055-
if not all(len(pair) == 2 for pair in pairs):
3056-
raise RuntimeError(
3057-
f'Invalid color names file {file!r}. '
3058-
f'Every line must be formatted as "name: color".'
3050+
cnt = 0
3051+
hex = re.compile(
3052+
r'\A#(?:[0-9a-fA-F]{3}){1,2}\Z' # ?: prevents capture
30593053
)
3054+
pairs = []
3055+
for line in f.readlines():
3056+
cnt += 1
3057+
stripped = line.strip()
3058+
if not stripped or stripped[0] == '#':
3059+
continue
3060+
pair = tuple(item.strip() for item in line.split(':'))
3061+
if len(pair) != 2 or not hex.match(pair[1]):
3062+
_warn_proplot(
3063+
f'Illegal line #{cnt} in file {file!r}:\n'
3064+
f'{line!r}\n'
3065+
f'Lines must be formatted as "name: hexcolor".'
3066+
)
3067+
continue
3068+
pairs.append(pair)
30603069

30613070
# Categories for which we add *all* colors
30623071
if cat == 'opencolor' or i == 1:

0 commit comments

Comments
 (0)