Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3284 vikas 1
/* Plugin for jQuery for working with colors.
2
 * 
3
 * Version 1.1.
4
 * 
5
 * Inspiration from jQuery color animation plugin by John Resig.
6
 *
7
 * Released under the MIT license by Ole Laursen, October 2009.
8
 *
9
 * Examples:
10
 *
11
 *   $.color.parse("#fff").scale('rgb', 0.25).add('a', -0.5).toString()
12
 *   var c = $.color.extract($("#mydiv"), 'background-color');
13
 *   console.log(c.r, c.g, c.b, c.a);
14
 *   $.color.make(100, 50, 25, 0.4).toString() // returns "rgba(100,50,25,0.4)"
15
 *
16
 * Note that .scale() and .add() return the same modified object
17
 * instead of making a new one.
18
 *
19
 * V. 1.1: Fix error handling so e.g. parsing an empty string does
20
 * produce a color rather than just crashing.
21
 */ 
22
 
23
(function($) {
24
    $.color = {};
25
 
26
    // construct color object with some convenient chainable helpers
27
    $.color.make = function (r, g, b, a) {
28
        var o = {};
29
        o.r = r || 0;
30
        o.g = g || 0;
31
        o.b = b || 0;
32
        o.a = a != null ? a : 1;
33
 
34
        o.add = function (c, d) {
35
            for (var i = 0; i < c.length; ++i)
36
                o[c.charAt(i)] += d;
37
            return o.normalize();
38
        };
39
 
40
        o.scale = function (c, f) {
41
            for (var i = 0; i < c.length; ++i)
42
                o[c.charAt(i)] *= f;
43
            return o.normalize();
44
        };
45
 
46
        o.toString = function () {
47
            if (o.a >= 1.0) {
48
                return "rgb("+[o.r, o.g, o.b].join(",")+")";
49
            } else {
50
                return "rgba("+[o.r, o.g, o.b, o.a].join(",")+")";
51
            }
52
        };
53
 
54
        o.normalize = function () {
55
            function clamp(min, value, max) {
56
                return value < min ? min: (value > max ? max: value);
57
            }
58
 
59
            o.r = clamp(0, parseInt(o.r), 255);
60
            o.g = clamp(0, parseInt(o.g), 255);
61
            o.b = clamp(0, parseInt(o.b), 255);
62
            o.a = clamp(0, o.a, 1);
63
            return o;
64
        };
65
 
66
        o.clone = function () {
67
            return $.color.make(o.r, o.b, o.g, o.a);
68
        };
69
 
70
        return o.normalize();
71
    }
72
 
73
    // extract CSS color property from element, going up in the DOM
74
    // if it's "transparent"
75
    $.color.extract = function (elem, css) {
76
        var c;
77
        do {
78
            c = elem.css(css).toLowerCase();
79
            // keep going until we find an element that has color, or
80
            // we hit the body
81
            if (c != '' && c != 'transparent')
82
                break;
83
            elem = elem.parent();
84
        } while (!$.nodeName(elem.get(0), "body"));
85
 
86
        // catch Safari's way of signalling transparent
87
        if (c == "rgba(0, 0, 0, 0)")
88
            c = "transparent";
89
 
90
        return $.color.parse(c);
91
    }
92
 
93
    // parse CSS color string (like "rgb(10, 32, 43)" or "#fff"),
94
    // returns color object, if parsing failed, you get black (0, 0,
95
    // 0) out
96
    $.color.parse = function (str) {
97
        var res, m = $.color.make;
98
 
99
        // Look for rgb(num,num,num)
100
        if (res = /rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(str))
101
            return m(parseInt(res[1], 10), parseInt(res[2], 10), parseInt(res[3], 10));
102
 
103
        // Look for rgba(num,num,num,num)
104
        if (res = /rgba\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]+(?:\.[0-9]+)?)\s*\)/.exec(str))
105
            return m(parseInt(res[1], 10), parseInt(res[2], 10), parseInt(res[3], 10), parseFloat(res[4]));
106
 
107
        // Look for rgb(num%,num%,num%)
108
        if (res = /rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(str))
109
            return m(parseFloat(res[1])*2.55, parseFloat(res[2])*2.55, parseFloat(res[3])*2.55);
110
 
111
        // Look for rgba(num%,num%,num%,num)
112
        if (res = /rgba\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\s*\)/.exec(str))
113
            return m(parseFloat(res[1])*2.55, parseFloat(res[2])*2.55, parseFloat(res[3])*2.55, parseFloat(res[4]));
114
 
115
        // Look for #a0b1c2
116
        if (res = /#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(str))
117
            return m(parseInt(res[1], 16), parseInt(res[2], 16), parseInt(res[3], 16));
118
 
119
        // Look for #fff
120
        if (res = /#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(str))
121
            return m(parseInt(res[1]+res[1], 16), parseInt(res[2]+res[2], 16), parseInt(res[3]+res[3], 16));
122
 
123
        // Otherwise, we're most likely dealing with a named color
124
        var name = $.trim(str).toLowerCase();
125
        if (name == "transparent")
126
            return m(255, 255, 255, 0);
127
        else {
128
            // default to black
129
            res = lookupColors[name] || [0, 0, 0];
130
            return m(res[0], res[1], res[2]);
131
        }
132
    }
133
 
134
    var lookupColors = {
135
        aqua:[0,255,255],
136
        azure:[240,255,255],
137
        beige:[245,245,220],
138
        black:[0,0,0],
139
        blue:[0,0,255],
140
        brown:[165,42,42],
141
        cyan:[0,255,255],
142
        darkblue:[0,0,139],
143
        darkcyan:[0,139,139],
144
        darkgrey:[169,169,169],
145
        darkgreen:[0,100,0],
146
        darkkhaki:[189,183,107],
147
        darkmagenta:[139,0,139],
148
        darkolivegreen:[85,107,47],
149
        darkorange:[255,140,0],
150
        darkorchid:[153,50,204],
151
        darkred:[139,0,0],
152
        darksalmon:[233,150,122],
153
        darkviolet:[148,0,211],
154
        fuchsia:[255,0,255],
155
        gold:[255,215,0],
156
        green:[0,128,0],
157
        indigo:[75,0,130],
158
        khaki:[240,230,140],
159
        lightblue:[173,216,230],
160
        lightcyan:[224,255,255],
161
        lightgreen:[144,238,144],
162
        lightgrey:[211,211,211],
163
        lightpink:[255,182,193],
164
        lightyellow:[255,255,224],
165
        lime:[0,255,0],
166
        magenta:[255,0,255],
167
        maroon:[128,0,0],
168
        navy:[0,0,128],
169
        olive:[128,128,0],
170
        orange:[255,165,0],
171
        pink:[255,192,203],
172
        purple:[128,0,128],
173
        violet:[128,0,128],
174
        red:[255,0,0],
175
        silver:[192,192,192],
176
        white:[255,255,255],
177
        yellow:[255,255,0]
178
    };
179
})(jQuery);