Skip to content

Commit

Permalink
'mask' takes a string or list of strings instead of multiple parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
facelessuser committed Mar 30, 2021
1 parent 82fbeec commit 37a42db
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 14 deletions.
10 changes: 5 additions & 5 deletions coloraide/colors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,14 @@ def mutate(self, color, data=None, alpha=util.DEF_ALPHA, *, filters=None, **kwar
self._attach(self._parse(color, data, alpha, filters=filters, **kwargs))
return self

def mask(self, *channels, invert=False, in_place=False):
def mask(self, channel, *, invert=False, in_place=False):
"""Mask color channels."""

clone = self.clone()
masks = set(channels)
for channel in self._color.CHANNEL_NAMES:
if (not invert and channel in masks) or (invert and channel not in masks):
clone.set(channel, util.NaN)
masks = set([channel] if isinstance(channel, str) else channel)
for name in self._color.CHANNEL_NAMES:
if (not invert and name in masks) or (invert and name not in masks):
clone.set(name, util.NaN)
if in_place:
self.update(clone)
return self
Expand Down
2 changes: 1 addition & 1 deletion docs/src/markdown/api/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ Parameters
:
Parameters | Defaults | Description
----------- | ------------- | -----------
`*channels` | | One or more channels to mask (or not mask if `invert` is `#!py3 True`).
`channel` | | A string specifying a channel, or a list of strings specifying multiple channels. Specified channels will be masked (or the only channels not masked if `invert` is `#!py3 True`).
`invert` | `#!py3 False` | Use inverse masking logic and mask all channels that are not specified.
`in_place` | `#!py3 False` | Boolean used to determine if the the current color should be modified "in place" or a new [`Color`](#color) object should be returned.

Expand Down
6 changes: 3 additions & 3 deletions docs/src/markdown/interpolation.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,11 @@ color2.coords()
color.mix(color2, space="hsl")
```

Technically, any channel can be set to `NaN`. This is basically what the [`mask`](./api/index.md#mask) method is used for. It
can set any and all specified channels to `NaN` for the purpose of restricting channels when interpolating:
Technically, any channel can be set to `NaN`. This is basically what the [`mask`](./api/index.md#mask) method is used
for. It can set any and all specified channels to `NaN` for the purpose of restricting channels when interpolating:

```color
Color('white').mask('red', 'green').coords()
Color('white').mask(['red', 'green']).coords()
```

Channels can also be set directly to `NaN`, but it must be done by instantiating a `Color` object with raw data or by
Expand Down
2 changes: 1 addition & 1 deletion docs/src/markdown/manipulation.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ allow for a user to quickly and easily mask one or more channels:

```color
Color('white').coords()
Color('white').mask('red', 'green').coords()
Color('white').mask(['red', 'green']).coords()
```

The `alpha` channel can also be masked:
Expand Down
8 changes: 4 additions & 4 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -758,23 +758,23 @@ def test_mix_mask(self):

c1 = Color("color(srgb 0.25 1 1)")
c2 = Color("color(srgb 0.75 0 0)")
self.assertColorEqual(c1.mix(c2.mask("red", "green")), Color("srgb", [0.25, 1, 0.5]))
self.assertColorEqual(c1.mix(c2.mask(["red", "green"])), Color("srgb", [0.25, 1, 0.5]))

def test_mix_mask_invert(self):
"""Test mix adjust method."""

c1 = Color("color(srgb 0.25 1 1)")
c2 = Color("color(srgb 0.75 0 0)")
self.assertColorEqual(c1.mix(c2.mask("green", "blue", invert=True)), Color("srgb", [0.25, 0.5, 0.5]))
self.assertColorEqual(c1.mix(c2.mask(["green", "blue"], invert=True)), Color("srgb", [0.25, 0.5, 0.5]))

c1 = Color("color(srgb 0.25 1 1)")
c2 = Color("color(srgb 0.75 0 0)")
self.assertColorEqual(c1.mask("green", "blue", invert=True).mix(c2), Color("srgb", [0.75, 0.5, 0.5]))
self.assertColorEqual(c1.mask(["green", "blue"], invert=True).mix(c2), Color("srgb", [0.75, 0.5, 0.5]))

c1 = Color("color(srgb 0.25 1 1)")
c2 = Color("color(srgb 0.75 0 0)")
self.assertColorEqual(
c1.mask("green", "blue", "alpha", invert=True).mix(c2.mask("green", "blue", "alpha", invert=True)),
c1.mask(["green", "blue", "alpha"], invert=True).mix(c2.mask(["green", "blue", "alpha"], invert=True)),
Color("srgb", [0.0, 0.5, 0.5])
)

Expand Down

0 comments on commit 37a42db

Please sign in to comment.