-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdetectColour.js
129 lines (116 loc) · 4.59 KB
/
detectColour.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* Detect Color Scheme
* @Author Gerrit van Huyssteen
* @Website www.gvh.co.za
* @Contributer TrevorSchuil
* https://github.com/trevone/detect-colour-scheme
*
* Abstract
* This is a concrete instance of the Scheme
*
* Usage
* var scheme = DetectColor.Load();
*
* Propterties
* colors - Array
* hasAlpha - Function
* toHex - Function
* rgbToHex - Function String
* removeDuplicates - Function Array
* draw - Function Array
*/
console.log("begin");
DetectColor = ( typeof DetectColor === 'undefined' ) ? {} : DetectColor;
( function (){
/*
* Constructor
*/
DetectColor.Load = function() {
this.colors = this.draw();
}
/*
* Extend DetectColor.Load with some default properties
*/
DetectColor.Load.prototype = {
/*
* Set up some constants
*/
colors : [],
attrs : ["border-color","background-color","color","font-color","outline-color"],
hexDigits : ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"],
/*
* Strip a string down to its color
* this.returnColor( someColor );
*/
returnColor: function( string ) {
string.match( / *\([^)]*\) */g );
},
/*
* this one needs an explaination
*/
groupColors: function ( matchColor ){
var matchCount = 0;
for( var i in this.colors ) if( this.colors[i] == matchColor ) matchCount++;
if( matchCount == 0 && !!matchColor ) this.colors.push( matchColor );
},
/*
* Checks a color string for the alpha attribute
* this.hasAlpha( someColor );
*/
hasAlpha: function ( attr ){
return ( attr.split(',')[3] == undefined) ? 'rgb' : 'rgba';
},
/*
* Converts single value to Hex
* this.toHex( 120 );
*/
toHex: function ( value ) {
return isNaN( value ) ? "00" : this.hexDigits[ ( value - value % 16 ) / 16 ] + this.hexDigits[ value % 16];
},
/*
* Converts rgb to Hex
* this.rgbToHex('rgb(120, 120, 0));
*/
rgbToHex: function ( rgb ) {
if( this.hasAlpha( rgb ) ) return rgb;
/* IE8 and below fail safe */
if( Object.prototype.toString.call( rgb.match( /(.*?)\((\d+), (\d+), (\d+)\)/ ) ) === '[object Array]' )
return '#'+this.toHex( rgb[2] ) + this.toHex( rgb[3] ) + this.toHex( rgb[4] );
},
/*
* Used to remove duplicated colors
* this.removeDuplicates(['#eee',#eee]);
*/
removeDuplicates: function ( array ) {
var i, uniques = [], object = {};
for ( var i = 0; i < array.length; i++ ) object[array[i]] = 0;
for ( var i in object ) uniques.push(i);
return uniques;
},
/*
* Called from the constructor and returns all the colors it found
* this.draw(); //only called from here
*/
draw: function(){
jQuery('*').each( function () {
for( var a in DetectColor.Load.prototype.attrs ){
var background = jQuery( this ).css(DetectColor.Load.prototype.attrs[a]);
/* IE8 and below fail safe */
( Object.prototype.toString.call( background ) === '[object Array]' ) ?
DetectColor.Load.prototype.groupColors( DetectColor.Load.prototype.returnColor( background )[0] ) : DetectColor.Load.prototype.groupColors( background );
}
});
jQuery('<div id="colors" />').appendTo('body');
DetectColor.Load.prototype.colors = DetectColor.Load.prototype.removeDuplicates ( DetectColor.Load.prototype.colors );
for( var i in DetectColor.Load.prototype.removeDuplicates ( DetectColor.Load.prototype.colors ) )
jQuery( '#colors' ).append( function() {
if( !!DetectColor.Load.prototype.colors[i] ){
var hex = ( DetectColor.Load.prototype.colors[i].match('#') != 'null' ) ?
DetectColor.Load.prototype.colors[i] : DetectColor.Load.prototype.rgbToHex( DetectColor.Load.prototype.hasAlpha( DetectColor.Load.prototype.colors[i] ) + '' + DetectColor.Load.prototype.colors[i] ) ;
return '<div class="color" style="background-color:' + hex + ';" />';
}
});
return DetectColor.Load.prototype.colors;
}
}
})()