PageSourceSearch

https://www.visiterouen.com/app/dist/rouen/addons/woody-library/st…/code/es-modules/Core/Color/Color.js

js visiterouen.com collected 2026-10-01 13:05:45 UTC 11,648 bytes, 394 lines download raw bytes

vendor: 7,923 bytes, lines 1-281
1/* *
2 *
3 *  (c) 2010-2024 Torstein Honsi
4 *
5 *  License: www.highcharts.com/license
6 *
7 *  !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
8 *
9 * */
10'use strict';
11import H from '../Globals.js';
12import U from '../Utilities.js';
13const { isNumber, merge, pInt, defined } = U;
14/* *
15 *
16 *  Class
17 *
18 * */
19/* eslint-disable valid-jsdoc */
20/**
21 * Handle color operations. Some object methods are chainable.
22 *
23 * @class
24 * @name Highcharts.Color
25 *
26 * @param {Highcharts.ColorType} input
27 * The input color in either rgba or hex format
28 */
29class Color {
30    /* *
31     *
32     *  Static Functions
33     *
34     * */
35    /**
36     * Creates a color instance out of a color string or object.
37     *
38     * @function Highcharts.Color.parse
39     *
40     * @param {Highcharts.ColorType} [input]
41     * The input color in either rgba or hex format.
42     *
43     * @return {Highcharts.Color}
44     * Color instance.
45     */
46    static parse(input) {
47        return input ? new Color(input) : Color.None;
48    }
49    /* *
50     *
51     *  Constructor
52     *
53     * */
54    constructor(input) {
55        this.rgba = [NaN, NaN, NaN, NaN];
56        this.input = input;
57        const GlobalColor = H.Color;
58        // Backwards compatibility, allow class overwrite
59        if (GlobalColor && GlobalColor !== Color) {
60            return new GlobalColor(input);
61        }
62        let result, rgba, i, parser;
63        // Gradients
64        if (typeof input === 'object' &&
65            typeof input.stops !== 'undefined') {
66            this.stops = input.stops.map((stop) => new Color(stop[1]));
67            // Solid colors
68        }
69        else if (typeof input === 'string') {
70            this.input = input = (Color.names[input.toLowerCase()] || input);
71            i = Color.parsers.length;
72            while (i-- && !rgba) {
73                parser = Color.parsers[i];
74                result = parser.regex.exec(input);
75                if (result) {
76                    rgba = parser.parse(result);
77                }
78            }
79        }
80        if (rgba) {
81            this.rgba = rgba;
82        }
83    }
84    /* *
85     *
86     *  Functions
87     *
88     * */
89    /**
90     * Return the color or gradient stops in the specified format
91     *
92     * @function Highcharts.Color#get
93     *
94     * @param {string} [format]
95     * Possible values are 'a', 'rgb', 'rgba' (default).
96     *
97     * @return {Highcharts.ColorType}
98     * This color as a string or gradient stops.
99     */
100    get(format) {
101        const input = this.input, rgba = this.rgba;
102        if (typeof input === 'object' &&
103            typeof this.stops !== 'undefined') {
104            const ret = merge(input);
105            ret.stops = [].slice.call(ret.stops);
106            this.stops.forEach((stop, i) => {
107                ret.stops[i] = [
108                    ret.stops[i][0],
109                    stop.get(format)
110                ];
111            });
112            return ret;
113        }
114        // It's NaN if gradient colors on a column chart
115        if (rgba && isNumber(rgba[0])) {
116            if (format === 'rgb' || (!format && rgba[3] === 1)) {
117                return 'rgb(' + rgba[0] + ',' + rgba[1] + ',' + rgba[2] + ')';
118            }
119            if (format === 'a') {
120                return `${rgba[3]}`;
121            }
122            return 'rgba(' + rgba.join(',') + ')';
123        }
124        return input;
125    }
126    /**
127     * Brighten the color instance.
128     *
129     * @function Highcharts.Color#brighten
130     *
131     * @param {number} alpha
132     * The alpha value.
133     *
134     * @return {Highcharts.Color}
135     * This color with modifications.
136     */
137    brighten(alpha) {
138        const rgba = this.rgba;
139        if (this.stops) {
140            this.stops.forEach(function (stop) {
141                stop.brighten(alpha);
142            });
143        }
144        else if (isNumber(alpha) && alpha !== 0) {
145            for (let i = 0; i < 3; i++) {
146                rgba[i] += pInt(alpha * 255);
147                if (rgba[i] < 0) {
148                    rgba[i] = 0;
149                }
150                if (rgba[i] > 255) {
151                    rgba[i] = 255;
152                }
153            }
154        }
155        return this;
156    }
157    /**
158     * Set the color's opacity to a given alpha value.
159     *
160     * @function Highcharts.Color#setOpacity
161     *
162     * @param {number} alpha
163     *        Opacity between 0 and 1.
164     *
165     * @return {Highcharts.Color}
166     *         Color with modifications.
167     */
168    setOpacity(alpha) {
169        this.rgba[3] = alpha;
170        return this;
171    }
172    /**
173     * Return an intermediate color between two colors.
174     *
175     * @function Highcharts.Color#tweenTo
176     *
177     * @param {Highcharts.Color} to
178     * The color object to tween to.
179     *
180     * @param {number} pos
181     * The intermediate position, where 0 is the from color (current color
182     * item), and 1 is the `to` color.
183     *
184     * @return {Highcharts.ColorType}
185     * The intermediate color in rgba notation, or unsupported type.
186     */
187    tweenTo(to, pos) {
188        const fromRgba = this.rgba, toRgba = to.rgba;
189        // Unsupported color, return to-color (#3920, #7034)
190        if (!isNumber(fromRgba[0]) || !isNumber(toRgba[0])) {
191            return to.input || 'none';
192        }
193        // Check for has alpha, because rgba colors perform worse due to
194        // lack of support in WebKit.
195        const hasAlpha = (toRgba[3] !== 1 || fromRgba[3] !== 1), channel = (to, i) => to + (fromRgba[i] - to) * (1 - pos), rgba = toRgba.slice(0, 3).map(channel).map(Math.round);
196        if (hasAlpha) {
197            rgba.push(channel(toRgba[3], 3));
198        }
199        return (hasAlpha ? 'rgba(' : 'rgb(') + rgba.join(',') + ')';
200    }
201}
202/* *
203 *
204 *  Static Properties
205 *
206 * */
207/**
208 * Collection of named colors. Can be extended from the outside by adding
209 * colors to Highcharts.Color.names.
210 * @private
211 */
212Color.names = {
213    white: '#ffffff',
214    black: '#000000'
215};
216/**
217 * Collection of parsers. This can be extended from the outside by pushing
218 * parsers to `Color.parsers`.
219 * @private
220 */
221Color.parsers = [{
222        // RGBA color
223        // eslint-disable-next-line max-len
224        regex: /rgba\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d?(?:\.\d+)?)\s*\)/,
225        parse: function (result) {
226            return [
227                pInt(result[1]),
228                pInt(result[2]),
229                pInt(result[3]),
230                parseFloat(result[4], 10)
231            ];
232        }
233    }, {
234        // RGB color
235        regex: /rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)/,
236        parse: function (result) {
237            return [pInt(result[1]), pInt(result[2]), pInt(result[3]), 1];
238        }
239    }, {
240        // RGBA 3 & 4 digit hex color, e.g. #F0F, #F0FA
241        regex: /^#([a-f0-9])([a-f0-9])([a-f0-9])([a-f0-9])?$/i,
242        parse: function (result) {
243            // #abcd => #aabbccdd, hence result + result.
244            return [
245                (pInt(result[1] + result[1], 16)),
246                (pInt(result[2] + result[2], 16)),
247                (pInt(result[3] + result[3], 16)),
248                !defined(result[4]) ?
249                    1 :
250                    (pInt(result[4] + result[4], 16) / 255)
251            ];
252        }
253    }, {
254        // RGBA 6 & 8 digit hex color, e.g. #FFCC00, #FFCC00FF
255        regex: /^#([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})?$/i,
256        parse: function (result) {
257            return [
258                pInt(result[1], 16),
259                pInt(result[2], 16),
260                pInt(result[3], 16),
261                !defined(result[4]) ?
262                    1 :
263                    (pInt(result[4], 16) / 255)
264            ];
265        }
266    }];
267// Must be last static member for init cycle
268Color.None = new Color('');
269/* *
270 *
271 *  Default Export
272 *
273 * */
274export default Color;
275/* *
276 *
277 *  API Declarations
278 *
279 * */
280/**
281 * A valid color to be parsed and handled by Highcharts. Highcharts internally
282 * supports hex colors like `#ffffff`, rgb colors like `rgb(255,255,255)` and
283 * rgba colors like `rgba(255,255,255,1)`. Other colors may be supported by the
284 * browsers and displayed correctly, but Highcharts is not able to process them
285 * and apply concepts like opacity and brightening.
286 *
287 * @typedef {string} Highcharts.ColorString
288 */
289/**
290 * A valid color type than can be parsed and handled by Highcharts. It can be a
291 * color string, a gradient object, or a pattern object.
292 *
293 * @typedef {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject} Highcharts.ColorType
294 */
295/**
296 * Gradient options instead of a solid color.
297 *
298 * @example
299 * // Linear gradient used as a color option
300 * color: {
301 *     linearGradient: { x1: 0, x2: 0, y1: 0, y2: 1 },
302 *     stops: [
303 *         [0, '#003399'], // start
304 *         [0.5, '#ffffff'], // middle
305 *         [1, '#3366AA'] // end
306 *     ]
307 * }
308 *
309 * @interface Highcharts.GradientColorObject
310 */ /**
311* Holds an object that defines the start position and the end position relative
312* to the shape.
313* @name Highcharts.GradientColorObject#linearGradient
314* @type {Highcharts.LinearGradientColorObject|undefined}
315*/ /**
316* Holds an object that defines the center position and the radius.
317* @name Highcharts.GradientColorObject#radialGradient
318* @type {Highcharts.RadialGradientColorObject|undefined}
319*/ /**
320* The first item in each tuple is the position in the gradient, where 0 is the
321* start of the gradient and 1 is the end of the gradient. Multiple stops can be
322* applied. The second item is the color for each stop. This color can also be
323* given in the rgba format.
324* @name Highcharts.GradientColorObject#stops
325* @type {Array<Highcharts.GradientColorStopObject>}
326*/
327/**
328 * Color stop tuple.
329 *
330 * @see Highcharts.GradientColorObject
331 *
332 * @interface Highcharts.GradientColorStopObject
333 */ /**
334* @name Highcharts.GradientColorStopObject#0
335* @type {number}
336*/ /**
337* @name Highcharts.GradientColorStopObject#1
338* @type {Highcharts.ColorString}
339*/ /**
340* @name Highcharts.GradientColorStopObject#color
341* @type {Highcharts.Color|undefined}
342*/
343/**
344 * Defines the start position and the end position for a gradient relative
345 * to the shape. Start position (x1, y1) and end position (x2, y2) are relative
346 * to the shape, where 0 means top/left and 1 is bottom/right.
347 *
348 * @interface Highcharts.LinearGradientColorObject
349 */ /**
350* Start horizontal position of the gradient. Float ranges 0-1.
351* @name Highcharts.LinearGradientColorObject#x1
352* @type {number}
353*/ /**
354* End horizontal position of the gradient. Float ranges 0-1.
355* @name Highcharts.LinearGradientColorObject#x2
356* @type {number}
357*/ /**
358* Start vertical position of the gradient. Float ranges 0-1.
359* @name Highcharts.LinearGradientColorObject#y1
360* @type {number}
361*/ /**
362* End vertical position of the gradient. Float ranges 0-1.
363* @name Highcharts.LinearGradientColorObject#y2
364* @type {number}
365*/
366/**
367 * Defines the center position and the radius for a gradient.
368 *
369 * @interface Highcharts.RadialGradientColorObject
370 */ /**
371* Center horizontal position relative to the shape. Float ranges 0-1.
372* @name Highcharts.RadialGradientColorObject#cx
373* @type {number}
374*/ /**
375* Center vertical position relative to the shape. Float ranges 0-1.
376* @name Highcharts.RadialGradientColorObject#cy
377* @type {number}
378*/ /**
379* Radius relative to the shape. Float ranges 0-1.
380* @name Highcharts.RadialGradientColorObject#r
381* @type {number}
382*/
383/**
384 * Creates a color instance out of a color string.
385 *
386 * @function Highcharts.color
387 *
388 * @param {Highcharts.ColorType} input
389 *        The input color in either rgba or hex format
390 *
391 * @return {Highcharts.Color}
392 *         Color instance
393 */
394(''); // Detach doclets above

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.