Skip to content

Commit c83c6ee

Browse files
committed
Fix parsing coords that have a minimum range < 0
The acescc color space has coords with a range of [-0.358, 1.468]. The percentageRange function of the Type class was returning a value of [-1, 1] which caused coords specified as percentages to be calculated incorrectly. The percentageRange function will now return [0, 1] if the Type has coordMeta with a range property.
1 parent a3e8958 commit c83c6ee

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

releases/v0.6.0.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ There is a number of breaking changes in this release, but they should only nega
1717
As [announced](https://github.com/color-js/color.js/releases/tag/v0.5.0) in v0.5.0, we have now switched to using `null` instead of `NaN` to represent `none` values (naturally occurring when converting achromatic colors to certain color spaces).
1818
Not only is `null` conceptually closer, but since [CSS *also* now has a `NaN` value](https://www.w3.org/TR/css-values-4/#calc-error-constants), this change allows us to represent it properly, using an actual `NaN` value.
1919

20-
`NaN` continues to be parsed (and becomes `NaN` in JS). Instead of being serialized to `NaN` (which is invalid in CSS), it is serialized to `calc(NaN)` which is a valid coordinate in CSS. For roundtripping to work properly, this also means we now parse `calc(NaN)` as well. Slippery slope? We’ll see. 😁
20+
`NaN` continues to be parsed (and becomes `NaN` in JS). Instead of being serialized to `NaN` (which is invalid in CSS), it is serialized to `calc(NaN)` which is a valid coordinate in CSS. For roundtripping to work properly, this also means we now parse `calc(NaN)` as well. Slippery slope? We’ll see. 😁
2121

2222
If you are working with any code that needs to handle `Color` instances/objects generically, without knowing which version of Color.js they come from, you can detect which value is being used and use that instead of a hardcoded `null` or `NaN`:
2323

@@ -84,7 +84,7 @@ If you were referencing these from their previous URL, there is a redirect in pl
8484
8585
## Color apps
8686
87-
We have now moved our [Color apps](https://apps.colorjs.io) (which also serve as Color.js demos) into their own repo and domain: https://apps.colorjs.io
87+
We have now moved our [Color apps](https://apps.colorjs.io) (which also serve as Color.js demos) into their own repo and domain: https://apps.colorjs.io
8888
8989
If you have links to these, there’s nothing to worry about: the old URL still works (it just redirects to the new one).
9090
@@ -129,6 +129,7 @@ There is also a new app:
129129
- Do not use HSL normalized saturation and hue for certain spaces (by @facelessuser in #582)
130130
- Avoid mutating arguments passed to the Color constructor (by @MysteryBlokHed in #603)
131131
- Fix parsing 7-character hex colors (by @kleinfreund in #616)
132+
- Fix parsing of percentage values for color spaces with coords that have a range property with a minimum value less than 0 (e.g. acescc) (by @lloydk in #619)
132133
133134
### Improvements to types
134135

src/Type.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export default class Type {
1818
}
1919

2020
if (coordMeta) {
21+
this.coordMeta = coordMeta;
2122
this.coordRange = coordMeta.range ?? coordMeta.refRange;
2223
}
2324

@@ -112,8 +113,14 @@ export default class Type {
112113
* @returns {[number, number]}
113114
*/
114115
percentageRange (scale = 1) {
115-
let range = this.coordRange && this.coordRange[0] < 0 ? [-1, 1] : [0, 1];
116-
return /** @type {[number, number]} */ (range.map(v => v * scale));
116+
let range;
117+
if ((this.coordMeta && this.coordMeta.range) || (this.coordRange && this.coordRange[0] >= 0)) {
118+
range = [0, 1];
119+
}
120+
else {
121+
range = [-1, 1];
122+
}
123+
return [range[0] * scale, range[1] * scale];
117124
}
118125

119126
static get (type, ...args) {

0 commit comments

Comments
 (0)