~ubuntuone-pqm-team/yui/stable-min

« back to all changes in this revision

Viewing changes to build/color-hsl/color-hsl.js

  • Committer: Ricardo Kirkner
  • Date: 2014-09-23 20:17:06 UTC
  • Revision ID: ricardo.kirkner@canonical.com-20140923201706-17kwxwckw6orp28k
re-added all .js files

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
YUI.add('color-hsl', function (Y, NAME) {
 
2
 
 
3
/**
 
4
Color provides static methods for color conversion to hsl values.
 
5
 
 
6
    Y.Color.toHSL('f00'); // hsl(0, 100%, 50%)
 
7
 
 
8
    Y.Color.toHSLA('rgb(255, 255, 0'); // hsla(60, 100%, 50%, 1)
 
9
 
 
10
@module color
 
11
@submodule color-hsl
 
12
@class HSL
 
13
@namespace Color
 
14
@since 3.8.0
 
15
**/
 
16
Color = {
 
17
 
 
18
    /**
 
19
    @static
 
20
    @property REGEX_HSL
 
21
    @type RegExp
 
22
    @default /hsla?\(([.\d]*), ?([.\d]*)%, ?([.\d]*)%,? ?([.\d]*)?\)/
 
23
    @since 3.8.0
 
24
    **/
 
25
    REGEX_HSL: /hsla?\(([.\d]*), ?([.\d]*)%, ?([.\d]*)%,? ?([.\d]*)?\)/,
 
26
 
 
27
    /**
 
28
    @static
 
29
    @property STR_HSL
 
30
    @type String
 
31
    @default hsl({*}, {*}%, {*}%)
 
32
    @since 3.8.0
 
33
    **/
 
34
    STR_HSL: 'hsl({*}, {*}%, {*}%)',
 
35
 
 
36
    /**
 
37
    @static
 
38
    @property STR_HSLA
 
39
    @type String
 
40
    @default hsla({*}, {*}%, {*}%, {*})
 
41
    @since 3.8.0
 
42
    **/
 
43
    STR_HSLA: 'hsla({*}, {*}%, {*}%, {*})',
 
44
 
 
45
    /**
 
46
    Converts provided color value to an HSL string.
 
47
    @public
 
48
    @method toHSL
 
49
    @param {String} str
 
50
    @return {String}
 
51
    @since 3.8.0
 
52
    **/
 
53
    toHSL: function (str) {
 
54
        var clr = Y.Color._convertTo(str, 'hsl');
 
55
        return clr.toLowerCase();
 
56
    },
 
57
 
 
58
    /**
 
59
    Converts provided color value to an HSLA string.
 
60
    @public
 
61
    @method toHSLA
 
62
    @param {String} str
 
63
    @return {String}
 
64
    @since 3.8.0
 
65
    **/
 
66
    toHSLA: function (str) {
 
67
        var clr = Y.Color._convertTo(str, 'hsla');
 
68
        return clr.toLowerCase();
 
69
    },
 
70
 
 
71
    /**
 
72
    Parses the RGB string into h, s, l values. Will return an Array
 
73
        of values or an HSL string.
 
74
    @protected
 
75
    @method _rgbToHsl
 
76
    @param {String} str
 
77
    @param {Boolean} [toArray]
 
78
    @return {String|Array}
 
79
    @since 3.8.0
 
80
    **/
 
81
    _rgbToHsl: function (str, toArray) {
 
82
        var h, s, l,
 
83
            rgb = Y.Color.REGEX_RGB.exec(str),
 
84
            r = rgb[1] / 255,
 
85
            g = rgb[2] / 255,
 
86
            b = rgb[3] / 255,
 
87
            max = Math.max(r, g, b),
 
88
            min = Math.min(r, g, b),
 
89
            isGrayScale = false,
 
90
            sub = max - min,
 
91
            sum = max + min;
 
92
 
 
93
 
 
94
        if (r === g && g === b) {
 
95
            isGrayScale = true;
 
96
        }
 
97
 
 
98
        // hue
 
99
        if (sub === 0) {
 
100
            h = 0;
 
101
        } else if (r === max) {
 
102
            h = ((60 * (g - b) / sub) + 360) % 360;
 
103
        } else if (g === max) {
 
104
            h = (60 * (b - r) / sub) + 120;
 
105
        } else {
 
106
            h = (60 * (r - g) / sub) + 240;
 
107
        }
 
108
 
 
109
        // lightness
 
110
        l = sum / 2;
 
111
 
 
112
        // saturation
 
113
        if (l === 0 || l === 1) {
 
114
            s = l;
 
115
        } else if (l <= 0.5) {
 
116
            s = sub / sum;
 
117
        } else {
 
118
            s = sub / (2 - sum);
 
119
        }
 
120
 
 
121
        if (isGrayScale) {
 
122
            s = 0;
 
123
        }
 
124
 
 
125
        // clean up hsl
 
126
        h = Math.round(h);
 
127
        s = Math.round(s * 100);
 
128
        l = Math.round(l * 100);
 
129
 
 
130
        if (toArray) {
 
131
            return [h, s, l];
 
132
        }
 
133
 
 
134
        return 'hsl(' + h + ', ' + s + '%, ' + l + '%)';
 
135
    },
 
136
 
 
137
    /**
 
138
    Parses the HSL string into r, b, g values. Will return an Array
 
139
        of values or an RGB string.
 
140
    @protected
 
141
    @method _hslToRgb
 
142
    @param {String} str
 
143
    @param {Boolean} [toArray]
 
144
    @return {String|Array}
 
145
    @since 3.8.0
 
146
    **/
 
147
    _hslToRgb: function (str, toArray) {
 
148
        // assume input is [h, s, l]
 
149
        // TODO: Find legals for use of formula
 
150
        var hsl = Y.Color.REGEX_HSL.exec(str),
 
151
            h = parseInt(hsl[1], 10) / 360,
 
152
            s = parseInt(hsl[2], 10) / 100,
 
153
            l = parseInt(hsl[3], 10) / 100,
 
154
            r,
 
155
            g,
 
156
            b,
 
157
            p,
 
158
            q;
 
159
 
 
160
        if (l <= 0.5) {
 
161
            q = l * (s + 1);
 
162
        } else {
 
163
            q = (l + s) - (l * s);
 
164
        }
 
165
 
 
166
        p = 2 * l - q;
 
167
 
 
168
        r = Math.round(Color._hueToRGB(p, q, h + 1/3) * 255);
 
169
        g = Math.round(Color._hueToRGB(p, q, h) * 255);
 
170
        b = Math.round(Color._hueToRGB(p, q, h - 1/3) * 255);
 
171
 
 
172
        if (toArray) {
 
173
            return [r, g, b];
 
174
        }
 
175
 
 
176
        return 'rgb(' + r + ', ' + g + ', ' + b + ')';
 
177
    },
 
178
 
 
179
    /**
 
180
    Converts the HSL hue to the different channels for RGB
 
181
 
 
182
    @protected
 
183
    @method _hueToRGB
 
184
    @param {Number} p
 
185
    @param {Number} q
 
186
    @param {Number} hue
 
187
    @return {Number} value for requested channel
 
188
    @since 3.8.0
 
189
    **/
 
190
    _hueToRGB: function(p, q, hue) {
 
191
        // TODO: Find legals for use of formula
 
192
        if (hue < 0) {
 
193
            hue += 1;
 
194
        } else if (hue > 1) {
 
195
            hue -= 1;
 
196
        }
 
197
 
 
198
        if (hue * 6 < 1) {
 
199
            return p + (q - p) * 6 * hue;
 
200
        }
 
201
        if (hue * 2 < 1) {
 
202
            return q;
 
203
        }
 
204
        if (hue * 3 < 2) {
 
205
            return p + (q - p) * (2/3 - hue) * 6;
 
206
        }
 
207
        return p;
 
208
    }
 
209
 
 
210
};
 
211
 
 
212
Y.Color = Y.mix(Color, Y.Color);
 
213
 
 
214
Y.Color.TYPES = Y.mix(Y.Color.TYPES, {'HSL':'hsl', 'HSLA':'hsla'});
 
215
Y.Color.CONVERTS = Y.mix(Y.Color.CONVERTS, {'hsl': 'toHSL', 'hsla': 'toHSLA'});
 
216
 
 
217
 
 
218
}, '@VERSION@', {"requires": ["color-base"]});