Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
YUI.add('color-hsv', function (Y, NAME) {
2
 
3
/**
4
Color provides static methods for color conversion hsv values.
5
 
6
    Y.Color.toHSV('f00'); // hsv(0, 100%, 100%)
7
 
8
    Y.Color.toHSVA('rgb(255, 255, 0'); // hsva(60, 100%, 100%, 1)
9
 
10
 
11
@module color
12
@submodule color-hsv
13
@class HSV
14
@namespace Color
15
@since 3.8.0
16
**/
17
Color = {
18
 
19
    /**
20
    @static
21
    @property REGEX_HSV
22
    @type RegExp
23
    @default /hsva?\(([.\d]*), ?([.\d]*)%, ?([.\d]*)%,? ?([.\d]*)?\)/
24
    @since 3.8.0
25
    **/
26
    REGEX_HSV: /hsva?\(([.\d]*), ?([.\d]*)%, ?([.\d]*)%,? ?([.\d]*)?\)/,
27
 
28
    /**
29
    @static
30
    @property STR_HSV
31
    @type String
32
    @default hsv({*}, {*}%, {*}%)
33
    @since 3.8.0
34
    **/
35
    STR_HSV: 'hsv({*}, {*}%, {*}%)',
36
 
37
    /**
38
    @static
39
    @property STR_HSVA
40
    @type String
41
    @default hsva({*}, {*}%, {*}%, {*})
42
    @since 3.8.0
43
    **/
44
    STR_HSVA: 'hsva({*}, {*}%, {*}%, {*})',
45
 
46
    /**
47
    Converts provided color value to an HSV string.
48
    @public
49
    @method toHSV
50
    @param {String} str
51
    @return {String}
52
    @since 3.8.0
53
    **/
54
    toHSV: function (str) {
55
        var clr = Y.Color._convertTo(str, 'hsv');
56
        return clr.toLowerCase();
57
    },
58
 
59
    /**
60
    Converts provided color value to an HSVA string.
61
    @public
62
    @method toHSVA
63
    @param {String} str
64
    @return {String}
65
    @since 3.8.0
66
    **/
67
    toHSVA: function (str) {
68
        var clr = Y.Color._convertTo(str, 'hsva');
69
        return clr.toLowerCase();
70
    },
71
 
72
    /**
73
    Parses the RGB string into h, s, v values. Will return an Array
74
        of values or an HSV string.
75
    @protected
76
    @method _rgbToHsv
77
    @param {String} str
78
    @param {Boolean} [toArray]
79
    @return {String|Array}
80
    @since 3.8.0
81
    **/
82
    _rgbToHsv: function (str, toArray) {
83
        var h, s, v,
84
            rgb = Y.Color.REGEX_RGB.exec(str),
85
            r = rgb[1] / 255,
86
            g = rgb[2] / 255,
87
            b = rgb[3] / 255,
88
            max = Math.max(r, g, b),
89
            min = Math.min(r, g, b),
90
            delta = max - min;
91
 
92
        if (max === min) {
93
            h = 0;
94
        } else if (max === r) {
95
            h = 60 * (g - b) / delta;
96
        } else if (max === g) {
97
            h = (60 * (b - r) / delta) + 120;
98
        } else { // max === b
99
            h = (60 * (r - g) / delta) + 240;
100
        }
101
 
102
        s = (max === 0) ? 0 : 1 - (min / max);
103
 
104
        // ensure h is between 0 and 360
105
        while (h < 0) {
106
            h += 360;
107
        }
108
        h %= 360;
109
        h = Math.round(h);
110
 
111
        // saturation is percentage
112
        s = Math.round(s * 100);
113
 
114
        // value is percentage
115
        v = Math.round(max * 100);
116
 
117
        if (toArray) {
118
            return [h, s, v];
119
        }
120
 
121
        return Y.Color.fromArray([h, s, v], Y.Color.TYPES.HSV);
122
    },
123
 
124
    /**
125
    Parses the HSV string into r, b, g values. Will return an Array
126
        of values or an RGB string.
127
    @protected
128
    @method _hsvToRgb
129
    @param {String} str
130
    @param {Boolean} [toArray]
131
    @return {String|Array}
132
    @since 3.8.0
133
    **/
134
    _hsvToRgb: function (str, toArray) {
135
        var hsv = Y.Color.REGEX_HSV.exec(str),
136
            h = parseInt(hsv[1], 10),
137
            s = parseInt(hsv[2], 10) / 100, // 0 - 1
138
            v = parseInt(hsv[3], 10) / 100, // 0 - 1
139
            r,
140
            g,
141
            b,
142
            i = Math.floor(h / 60) % 6,
143
            f = (h / 60) - i,
144
            p = v * (1 - s),
145
            q = v * (1 - (s * f)),
146
            t = v * (1 - (s * (1 - f)));
147
 
148
        if (s === 0) {
149
            r = v;
150
            g = v;
151
            b = v;
152
        } else {
153
            switch (i) {
154
                case 0: r = v; g = t; b = p; break;
155
                case 1: r = q; g = v; b = p; break;
156
                case 2: r = p; g = v; b = t; break;
157
                case 3: r = p; g = q; b = v; break;
158
                case 4: r = t; g = p; b = v; break;
159
                case 5: r = v; g = p; b = q; break;
160
            }
161
        }
162
 
163
        r = Math.min(255, Math.round(r * 256));
164
        g = Math.min(255, Math.round(g * 256));
165
        b = Math.min(255, Math.round(b * 256));
166
 
167
        if (toArray) {
168
            return [r, g, b];
169
        }
170
 
171
        return Y.Color.fromArray([r, g, b], Y.Color.TYPES.RGB);
172
    }
173
 
174
};
175
 
176
Y.Color = Y.mix(Color, Y.Color);
177
 
178
Y.Color.TYPES = Y.mix(Y.Color.TYPES, {'HSV':'hsv', 'HSVA':'hsva'});
179
Y.Color.CONVERTS = Y.mix(Y.Color.CONVERTS, {'hsv': 'toHSV', 'hsva': 'toHSVA'});
180
 
181
 
182
}, '3.18.1', {"requires": ["color-base"]});