| 1 |
/** |
| 2 |
* This plugin defines getters and setters for color[spaceId] |
| 3 |
* e.g. color.lch on *any* color gives us the lch coords |
| 4 |
*/ |
| 5 |
import ColorSpace from "./ColorSpace.js"; |
| 6 |
import Color from "./color.js"; |
| 7 |
import hooks from "./hooks.js"; |
| 8 |
|
| 9 |
// Add space accessors to existing color spaces |
| 10 |
for (let id in ColorSpace.registry) { |
| 11 |
addSpaceAccessors(id, ColorSpace.registry[id]); |
| 12 |
} |
| 13 |
|
| 14 |
// Add space accessors to color spaces not yet created |
| 15 |
hooks.add("colorspace-init-end", space => { |
| 16 |
addSpaceAccessors(space.id, space); |
| 17 |
space.aliases?.forEach(alias => { |
| 18 |
addSpaceAccessors(alias, space); |
| 19 |
}); |
| 20 |
}); |
| 21 |
|
| 22 |
function addSpaceAccessors (id, space) { |
| 23 |
let propId = id.replace(/-/g, "_"); |
| 24 |
|
| 25 |
Object.defineProperty(Color.prototype, propId, { |
| 26 |
// Convert coords to coords in another colorspace and return them |
| 27 |
// Source colorspace: this.spaceId |
| 28 |
// Target colorspace: id |
| 29 |
get () { |
| 30 |
let ret = this.getAll(id); |
| 31 |
|
| 32 |
if (typeof Proxy === "undefined") { |
| 33 |
// If proxies are not supported, just return a static array |
| 34 |
return ret; |
| 35 |
} |
| 36 |
|
| 37 |
// Enable color.spaceId.coordName syntax |
| 38 |
let proxy = new Proxy(ret, { |
| 39 |
has: /** @param {string} property */ (obj, property) => { |
| 40 |
try { |
| 41 |
ColorSpace.resolveCoord([space, property]); |
| 42 |
return true; |
| 43 |
} |
| 44 |
catch (e) {} |
| 45 |
|
| 46 |
return Reflect.has(obj, property); |
| 47 |
}, |
| 48 |
get: (obj, property, receiver) => { |
| 49 |
if ( |
| 50 |
property && |
| 51 |
typeof property !== "symbol" && |
| 52 |
!(property in obj) && |
| 53 |
property in proxy |
| 54 |
) { |
| 55 |
let { index } = ColorSpace.resolveCoord([space, property]); |
| 56 |
|
| 57 |
if (index >= 0) { |
| 58 |
return obj[index]; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
return Reflect.get(obj, property, receiver); |
| 63 |
}, |
| 64 |
set: (obj, property, value, receiver) => { |
| 65 |
if ( |
| 66 |
(property && typeof property !== "symbol" && !(property in obj)) || |
| 67 |
Number(property) >= 0 |
| 68 |
) { |
| 69 |
let { index } = ColorSpace.resolveCoord([ |
| 70 |
space, |
| 71 |
/** @type {string} */ (property), |
| 72 |
]); |
| 73 |
|
| 74 |
if (index >= 0) { |
| 75 |
obj[index] = value; |
| 76 |
|
| 77 |
// Update color.coords |
| 78 |
this.setAll(id, obj); |
| 79 |
|
| 80 |
return true; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
return Reflect.set(obj, property, value, receiver); |
| 85 |
}, |
| 86 |
}); |
| 87 |
|
| 88 |
return proxy; |
| 89 |
}, |
| 90 |
// Convert coords in another colorspace to internal coords and set them |
| 91 |
// Target colorspace: this.spaceId |
| 92 |
// Source colorspace: id |
| 93 |
set (coords) { |
| 94 |
this.setAll(id, coords); |
| 95 |
}, |
| 96 |
configurable: true, |
| 97 |
enumerable: true, |
| 98 |
}); |
| 99 |
} |