Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
// This file is part of Moodle - http://moodle.org/
2
//
3
// Moodle is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, either version 3 of the License, or
6
// (at your option) any later version.
7
//
8
// Moodle is distributed in the hope that it will be useful,
9
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
// GNU General Public License for more details.
12
//
13
// You should have received a copy of the GNU General Public License
14
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
15
 
16
/*
17
 * @package    tiny_accessibilitychecker
18
 * @copyright  2022, Stevani Andolo  <stevani@hotmail.com.au>
19
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
20
 */
21
 
22
export default class {
23
 
24
    REGEX_HEX = /^#?([\da-fA-F]{2})([\da-fA-F]{2})([\da-fA-F]{2})(\ufffe)?/;
25
    REGEX_HEX3 = /^#?([\da-fA-F]{1})([\da-fA-F]{1})([\da-fA-F]{1})(\ufffe)?/;
26
    REGEX_RGB = /rgba?\(([\d]{1,3}), ?([\d]{1,3}), ?([\d]{1,3}),? ?([.\d]*)?\)/;
27
 
28
    TYPES = {
29
        HEX: 'hex',
30
        RGB: 'rgb',
31
        RGBA: 'rgba'
32
    };
33
 
34
    KEYWORDS = {
35
        black: '000',
36
        silver: 'c0c0c0',
37
        gray: '808080',
38
        white: 'fff',
39
        maroon: '800000',
40
        red: 'f00',
41
        purple: '800080',
42
        fuchsia: 'f0f',
43
        green: '008000',
44
        lime: '0f0',
45
        olive: '808000',
46
        yellow: 'ff0',
47
        navy: '000080',
48
        blue: '00f',
49
        teal: '008080',
50
        aqua: '0ff'
51
    };
52
 
53
    STR_HEX = '#{*}{*}{*}';
54
    STR_RGB = 'rgb({*}, {*}, {*})';
55
    STR_RGBA = 'rgba({*}, {*}, {*}, {*})';
56
 
57
    toHex = (str) => {
58
        var clr = this._convertTo(str, 'hex'),
59
            isTransparent = clr.toLowerCase() === 'transparent';
60
 
61
        if (clr.charAt(0) !== '#' && !isTransparent) {
62
            clr = '#' + clr;
63
        }
64
 
65
        return isTransparent ? clr.toLowerCase() : clr.toUpperCase();
66
    };
67
 
68
    toRGB = (str) => {
69
        var clr = this._convertTo(str, 'rgb');
70
        return clr.toLowerCase();
71
    };
72
 
73
    toRGBA = (str) => {
74
        var clr = this._convertTo(str, 'rgba');
75
        return clr.toLowerCase();
76
    };
77
 
78
    toArray = (str) => {
79
        // Parse with regex and return "matches" array.
80
        var type = this.findType(str).toUpperCase(),
81
            regex,
82
            arr,
83
            length,
84
            lastItem;
85
 
86
        if (type === 'HEX' && str.length < 5) {
87
            type = 'HEX3';
88
        }
89
 
90
        if (type.charAt(type.length - 1) === 'A') {
91
            type = type.slice(0, -1);
92
        }
93
 
94
        regex = this._getRightValue('REGEX_' + type);
95
 
96
        if (regex) {
97
            arr = regex.exec(str) || [];
98
            length = arr.length;
99
 
100
            if (length) {
101
                arr.shift();
102
                length--;
103
 
104
                if (type === 'HEX3') {
105
                    arr[0] += arr[0];
106
                    arr[1] += arr[1];
107
                    arr[2] += arr[2];
108
                }
109
                lastItem = arr[length - 1];
110
 
111
                if (!lastItem) {
112
                    arr[length - 1] = 1;
113
                }
114
            }
115
        }
116
        return arr;
117
    };
118
 
119
    fromArray = (arr, template) => {
120
        arr = arr.concat();
121
 
122
        if (typeof template === 'undefined') {
123
            return arr.join(', ');
124
        }
125
 
126
        var replace = '{*}';
127
        template = this._getRightValue('STR_' + template.toUpperCase());
128
 
129
        if (arr.length === 3 && template.match(/\{\*\}/g).length === 4) {
130
            arr.push(1);
131
        }
132
 
133
        while (template.indexOf(replace) >= 0 && arr.length > 0) {
134
            template = template.replace(replace, arr.shift());
135
        }
136
        return template;
137
    };
138
 
139
    findType = (str) => {
140
        if (this.KEYWORDS[str]) {
141
            return 'keyword';
142
        }
143
 
144
        var index = str.indexOf('('),
145
            key;
146
 
147
        if (index > 0) {
148
            key = str.substr(0, index);
149
        }
150
 
151
        if (key && this.TYPES[key.toUpperCase()]) {
152
            return this.TYPES[key.toUpperCase()];
153
        }
154
        return 'hex';
155
    };
156
 
157
    _getAlpha = (clr) => {
158
        var alpha,
159
            arr = this.toArray(clr);
160
 
161
        if (arr.length > 3) {
162
            alpha = arr.pop();
163
        }
164
        return +alpha || 1;
165
    };
166
 
167
    _keywordToHex = (clr) => {
168
        var keyword = this.KEYWORDS[clr];
169
 
170
        if (keyword) {
171
            return keyword;
172
        }
173
        return keyword;
174
    };
175
 
176
    _convertTo = (clr, to) => {
177
        if (clr === 'transparent') {
178
            return clr;
179
        }
180
 
181
        var from = this.findType(clr),
182
            originalTo = to,
183
            needsAlpha,
184
            alpha,
185
            method,
186
            ucTo;
187
 
188
        if (from === 'keyword') {
189
            clr = this._keywordToHex(clr);
190
            from = 'hex';
191
        }
192
 
193
        if (from === 'hex' && clr.length < 5) {
194
            if (clr.charAt(0) === '#') {
195
                clr = clr.substr(1);
196
            }
197
 
198
            clr = '#' + clr.charAt(0) + clr.charAt(0) +
199
                        clr.charAt(1) + clr.charAt(1) +
200
                        clr.charAt(2) + clr.charAt(2);
201
        }
202
 
203
        if (from === to) {
204
            return clr;
205
        }
206
 
207
        if (from.charAt(from.length - 1) === 'a') {
208
            from = from.slice(0, -1);
209
        }
210
 
211
        needsAlpha = (to.charAt(to.length - 1) === 'a');
212
        if (needsAlpha) {
213
            to = to.slice(0, -1);
214
            alpha = this._getAlpha(clr);
215
        }
216
 
217
        ucTo = to.charAt(0).toUpperCase() + to.substr(1).toLowerCase();
218
        method = window['_' + from + 'To' + ucTo];
219
 
220
        // Check to see if need conversion to rgb first.
221
        // Check to see if there is a direct conversion method.
222
        // Convertions are: hex <-> rgb <-> hsl.
223
        if (!method) {
224
            if (from !== 'rgb' && to !== 'rgb') {
225
                clr = window['_' + from + 'ToRgb'](clr);
226
                from = 'rgb';
227
                method = window['_' + from + 'To' + ucTo];
228
            }
229
        }
230
 
231
        if (method) {
232
            clr = ((method)(clr, needsAlpha));
233
        }
234
 
235
        // Process clr from arrays to strings after conversions if alpha is needed.
236
        if (needsAlpha) {
237
            if (!Array.isArray(clr)) {
238
                clr = this.toArray(clr);
239
            }
240
            clr.push(alpha);
241
            clr = this.fromArray(clr, originalTo.toUpperCase());
242
        }
243
        return clr;
244
    };
245
 
246
    _hexToRgb = (str, array) => {
247
        var r, g, b;
248
 
249
        /* jshint bitwise:false */
250
        if (str.charAt(0) === '#') {
251
            str = str.substr(1);
252
        }
253
 
254
        /* eslint no-bitwise: */
255
        str = parseInt(str, 16);
256
        r = str >> 16;
257
        g = str >> 8 & 0xFF;
258
        b = str & 0xFF;
259
 
260
        if (array) {
261
            return [r, g, b];
262
        }
263
        return 'rgb(' + r + ', ' + g + ', ' + b + ')';
264
    };
265
 
266
    _rgbToHex = (str) => {
267
        /* jshint bitwise:false */
268
        var rgb = this.toArray(str),
269
            hex = rgb[2] | (rgb[1] << 8) | (rgb[0] << 16);
270
 
271
        hex = (+hex).toString(16);
272
 
273
        while (hex.length < 6) {
274
            hex = '0' + hex;
275
        }
276
        return '#' + hex;
277
    };
278
 
279
    _getRightValue = (string) => {
280
        let regex = null;
281
        if (string === 'REGEX_RGB') {
282
            regex = this.REGEX_RGB;
283
        } else if (string === 'REGEX_HEX') {
284
            regex = this.REGEX_HEX;
285
        } else if (string === 'REGEX_HEX3') {
286
            regex = this.REGEX_HEX3;
287
        } else if (string === 'STR_HEX') {
288
            regex = this.STR_HEX;
289
        } else if (string === 'STR_RGB') {
290
            regex = this.STR_RGB;
291
        } else if (string === 'STR_RGBA') {
292
            regex = this.STR_RGBA;
293
        }
294
        return regex;
295
    };
296
}