Skip to content

Commit 80f68f3

Browse files
Feature/allow clear color (#63)
Added a new flag to named_colors to allow the use of clear colors. This is useful when "ignore_alpha" is too permissive. Also changed the format of the error message for "allowed_colors" which were printed with the unicode (u') prefix.
1 parent e71de58 commit 80f68f3

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ in the .xib or .storyboard file.
5050

5151
- `named_colors`
5252

53-
Ensures all colors are using named colors from an asset catalog. Configure `allowed_colors` to limit the colors to a subset (default is all named colors are allowed), `allow_system_colors` (default is `false`) and `ignore_alpha` (default is `false`) in a custom rule configuration using `rules_config` (see below).
53+
Ensures all colors are using named colors from an asset catalog. Configure `allowed_colors` to limit the colors to a subset (default is all named colors are allowed), `allow_system_colors` (default is `false`), `allow_clear_color` (default is `false`) and `ignore_alpha` (default is `false`) in a custom rule configuration using `rules_config` (see below).
5454

5555
- `no_trait_variations`
5656

xiblint/rules/named_colors.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,38 @@ class NamedColors(Rule):
1010
{
1111
"allowed_colors": ["CustomRed", "CustomGreen"],
1212
"allow_system_colors": true,
13-
"ignore_alpha": true
13+
"ignore_alpha": true,
14+
"allow_clear_color": true
1415
}
1516
"""
1617
def check(self, context): # type: (XibContext) -> None
1718
allowed_colors = self.config.get('allowed_colors', [])
1819
allow_system_colors = self.config.get('allow_system_colors', False)
1920
ignore_alpha = self.config.get('ignore_alpha', False)
21+
allow_clear_color = self.config.get('allow_clear_color', False)
2022

2123
for element in context.tree.findall(".//color"):
2224
# Skip <color> tags nested in a localization comment
2325
container = element.parent.parent.parent
2426
if container.tag == 'attributedString' and container.get('key') == 'userComments':
2527
continue
2628

27-
# Skip <color> tags part of a color definition
28-
if element.parent.tag == 'namedColor':
29-
continue
30-
31-
# Skip <color> tags part of a system color definition
32-
if element.parent.tag == 'systemColor':
29+
# Skip <color> tags part of a color or system color definition
30+
if element.parent.tag == 'namedColor' or element.parent.tag == 'systemColor':
3331
continue
3432

3533
# Skip colors with alpha (if configured)
3634
if ignore_alpha and element.get('alpha') is not None and element.get('alpha') != '1':
3735
continue
3836

37+
# Skip clear color (if configured)
38+
if (
39+
allow_clear_color and
40+
element.get('alpha') is not None and element.get('white') is not None and
41+
element.get('alpha') == '0.0' and element.get('white') == '0.0'
42+
):
43+
continue
44+
3945
# If `systemColor` or `catalog` is present, it's a named system color
4046
if (
4147
element.get('systemColor') is not None or
@@ -53,6 +59,7 @@ def check(self, context): # type: (XibContext) -> None
5359
continue
5460

5561
# If allowed_colors is set, verify that color_name is included
62+
options_string = '`, `'.join(map(str, allowed_colors))
5663
if allowed_colors and color_name not in allowed_colors:
57-
context.error(element, '"{}" is not one of the allowed colors: "{}".'
58-
.format(color_name, allowed_colors))
64+
context.error(element, '"{}" is not one of the allowed colors: `{}`.'
65+
.format(color_name, options_string))

0 commit comments

Comments
 (0)