| 1 |
/** |
| 2 |
* @packageDocumentation |
| 3 |
* @class Color |
| 4 |
* Class that represents a single color. |
| 5 |
* All of Color.js’s tree-shakeable methods are also available as instance methods on this class, |
| 6 |
* as well as static methods that take the color as the first argument. |
| 7 |
*/ |
| 8 |
|
| 9 |
import * as util from "./util.js"; |
| 10 |
import hooks from "./hooks.js"; |
| 11 |
import defaults from "./defaults.js"; |
| 12 |
import ColorSpace from "./ColorSpace.js"; |
| 13 |
import { WHITES } from "./adapt.js"; |
| 14 |
import { |
| 15 |
getColor, |
| 16 |
tryColor, |
| 17 |
parse, |
| 18 |
to, |
| 19 |
serialize, |
| 20 |
inGamut, |
| 21 |
toGamut, |
| 22 |
distance, |
| 23 |
deltas, |
| 24 |
equals, |
| 25 |
get, |
| 26 |
getAll, |
| 27 |
set, |
| 28 |
setAll, |
| 29 |
display, |
| 30 |
} from "./index-fn.js"; |
| 31 |
|
| 32 |
import "./spaces/xyz-d50.js"; |
| 33 |
import "./spaces/srgb.js"; |
| 34 |
|
| 35 |
export default class Color { |
| 36 |
/** |
| 37 |
* Creates an instance of Color. |
| 38 |
* Signatures: |
| 39 |
* - `new Color(stringToParse)` |
| 40 |
* - `new Color(otherColor)` |
| 41 |
* - `new Color({space, coords, alpha})` |
| 42 |
* - `new Color(space, coords, alpha)` |
| 43 |
* - `new Color(spaceId, coords, alpha)` |
| 44 |
*/ |
| 45 |
constructor (...args) { |
| 46 |
let color; |
| 47 |
|
| 48 |
if (args.length === 1) { |
| 49 |
let parseMeta = {}; |
| 50 |
// Clone simple objects to avoid mutating original in getColor |
| 51 |
if ( |
| 52 |
typeof args[0] === "object" && |
| 53 |
Object.getPrototypeOf(args[0]).constructor === Object |
| 54 |
) { |
| 55 |
args[0] = { ...args[0] }; |
| 56 |
} |
| 57 |
color = getColor(args[0], { parseMeta }); |
| 58 |
|
| 59 |
if (parseMeta.format) { |
| 60 |
// Color actually came from a string |
| 61 |
this.parseMeta = parseMeta; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
let space, coords, alpha; |
| 66 |
|
| 67 |
if (color) { |
| 68 |
space = color.space || color.spaceId; |
| 69 |
coords = color.coords; |
| 70 |
alpha = color.alpha; |
| 71 |
} |
| 72 |
else { |
| 73 |
// default signature new Color(ColorSpace, array [, alpha]) |
| 74 |
[space, coords, alpha] = args; |
| 75 |
} |
| 76 |
|
| 77 |
Object.defineProperty(this, "space", { |
| 78 |
value: ColorSpace.get(space), |
| 79 |
writable: false, |
| 80 |
enumerable: true, |
| 81 |
configurable: true, // see note in https://262.ecma-international.org/8.0/#sec-proxy-object-internal-methods-and-internal-slots-get-p-receiver |
| 82 |
}); |
| 83 |
|
| 84 |
this.coords = coords ? coords.slice() : [0, 0, 0]; |
| 85 |
|
| 86 |
// Clamp alpha to [0, 1] |
| 87 |
this.alpha = util.isNone(alpha) ? alpha : alpha === undefined ? 1 : util.clamp(0, alpha, 1); |
| 88 |
|
| 89 |
// Define getters and setters for each coordinate |
| 90 |
for (let id in this.space.coords) { |
| 91 |
Object.defineProperty(this, id, { |
| 92 |
get: () => this.get(id), |
| 93 |
set: value => this.set(id, value), |
| 94 |
}); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
get spaceId () { |
| 99 |
return this.space.id; |
| 100 |
} |
| 101 |
|
| 102 |
clone () { |
| 103 |
return new Color(this.space, this.coords, this.alpha); |
| 104 |
} |
| 105 |
|
| 106 |
toJSON () { |
| 107 |
return { |
| 108 |
spaceId: this.spaceId, |
| 109 |
coords: this.coords, |
| 110 |
alpha: this.alpha, |
| 111 |
}; |
| 112 |
} |
| 113 |
|
| 114 |
display (...args) { |
| 115 |
let ret = display(this, ...args); |
| 116 |
|
| 117 |
// Convert color object to Color instance |
| 118 |
ret.color = new Color(ret.color); |
| 119 |
|
| 120 |
return ret; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Get a color from the argument(s) passed |
| 125 |
* Basically gets us the same result as new Color(color) but doesn't clone an existing color object |
| 126 |
*/ |
| 127 |
static get (color, ...args) { |
| 128 |
if (util.isInstance(color, this)) { |
| 129 |
return color; |
| 130 |
} |
| 131 |
|
| 132 |
return new Color(color, ...args); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Get a color instance from the argument passed or `null` if resolution fails (instead of throwing an error). |
| 137 |
* Additionally, it supports passing an element to resolve complex CSS colors through the DOM (slow). |
| 138 |
* @see {@link tryColor} for more details |
| 139 |
*/ |
| 140 |
static try (color, options) { |
| 141 |
if (util.isInstance(color, this)) { |
| 142 |
return color; |
| 143 |
} |
| 144 |
|
| 145 |
let ret = tryColor(color, options); |
| 146 |
|
| 147 |
if (ret) { |
| 148 |
return new Color(ret); |
| 149 |
} |
| 150 |
|
| 151 |
return null; |
| 152 |
} |
| 153 |
|
| 154 |
static defineFunction (name, code, o = code) { |
| 155 |
let { instance = true, returns } = o; |
| 156 |
|
| 157 |
let func = function (...args) { |
| 158 |
let ret = code(...args); |
| 159 |
|
| 160 |
if (returns === "color") { |
| 161 |
ret = Color.get(ret); |
| 162 |
} |
| 163 |
else if (returns === "function<color>") { |
| 164 |
let f = ret; |
| 165 |
ret = function (...args) { |
| 166 |
let ret = f(...args); |
| 167 |
return Color.get(ret); |
| 168 |
}; |
| 169 |
// Copy any function metadata |
| 170 |
Object.assign(ret, f); |
| 171 |
} |
| 172 |
else if (returns === "array<color>") { |
| 173 |
ret = ret.map(c => Color.get(c)); |
| 174 |
} |
| 175 |
|
| 176 |
return ret; |
| 177 |
}; |
| 178 |
|
| 179 |
if (!(name in Color)) { |
| 180 |
Color[name] = func; |
| 181 |
} |
| 182 |
|
| 183 |
if (instance) { |
| 184 |
Color.prototype[name] = function (...args) { |
| 185 |
return func(this, ...args); |
| 186 |
}; |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
static defineFunctions (o) { |
| 191 |
for (let name in o) { |
| 192 |
Color.defineFunction(name, o[name], o[name]); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
static extend (exports) { |
| 197 |
if (exports.register) { |
| 198 |
exports.register(Color); |
| 199 |
} |
| 200 |
else { |
| 201 |
// No register method, just add the module's functions |
| 202 |
for (let name in exports) { |
| 203 |
Color.defineFunction(name, exports[name]); |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
Color.defineFunctions({ |
| 210 |
get, |
| 211 |
getAll, |
| 212 |
set, |
| 213 |
setAll, |
| 214 |
to, |
| 215 |
equals, |
| 216 |
inGamut, |
| 217 |
toGamut, |
| 218 |
distance, |
| 219 |
deltas, |
| 220 |
toString: serialize, |
| 221 |
}); |
| 222 |
|
| 223 |
Object.assign(Color, { |
| 224 |
util, |
| 225 |
hooks, |
| 226 |
WHITES, |
| 227 |
Space: ColorSpace, |
| 228 |
spaces: ColorSpace.registry, |
| 229 |
parse, |
| 230 |
|
| 231 |
// Global defaults one may want to configure |
| 232 |
defaults, |
| 233 |
}); |