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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
|
/*! Iris Color Picker - v1.0.4 - 2013-11-18
* https://github.com/Automattic/Iris
* Copyright (c) 2013 Matt Wiebe; Licensed GPLv2 */
(function( $, undef ){
var _html, nonGradientIE, gradientType, vendorPrefixes, _css, Iris, UA, isIE, IEVersion;
_html = '<div class="iris-picker"><div class="iris-picker-inner"><div class="iris-square"><a class="iris-square-value" href="#"><span class="iris-square-handle ui-slider-handle"></span></a><div class="iris-square-inner iris-square-horiz"></div><div class="iris-square-inner iris-square-vert"></div></div><div class="iris-slider iris-strip"><div class="iris-slider-offset"></div></div></div></div>';
// Even IE9 dosen't support gradients. Elaborate sigh.
UA = navigator.userAgent.toLowerCase();
isIE = navigator.appName === 'Microsoft Internet Explorer';
IEVersion = isIE ? parseFloat( UA.match( /msie ([0-9]{1,}[\.0-9]{0,})/ )[1] ) : 0;
nonGradientIE = ( isIE && IEVersion < 10 );
gradientType = false;
// we don't bother with an unprefixed version, as it has a different syntax
vendorPrefixes = [ '-moz-', '-webkit-', '-o-', '-ms-' ];
// This is manually copied from iris.min.css until I can figure out how to do it without
_css = '.iris-picker{display:block;position:relative}.iris-picker,.iris-picker *{-moz-box-sizing:content-box;-webkit-box-sizing:content-box;box-sizing:content-box}input+.iris-picker{margin-top:4px}.iris-error{background-color:#ffafaf}.iris-border{border-radius:3px;border:1px solid #aaa;width:200px;background-color:#fff}.iris-picker-inner{position:absolute;top:0;right:0;left:0;bottom:0}.iris-border .iris-picker-inner{top:10px;right:10px;left:10px;bottom:10px}.iris-picker .iris-square-inner{position:absolute;left:0;right:0;top:0;bottom:0}.iris-picker .iris-square,.iris-picker .iris-slider,.iris-picker .iris-square-inner,.iris-picker .iris-palette{border-radius:3px;box-shadow:inset 0 0 5px rgba(0,0,0,.4);height:100%;width:12.5%;float:left;margin-right:5%}.iris-picker .iris-square{width:76%;margin-right:10%;position:relative}.iris-picker .iris-square-inner{width:auto;margin:0}.iris-ie-9 .iris-square,.iris-ie-9 .iris-slider,.iris-ie-9 .iris-square-inner,.iris-ie-9 .iris-palette{box-shadow:none;border-radius:0}.iris-ie-9 .iris-square,.iris-ie-9 .iris-slider,.iris-ie-9 .iris-palette{outline:1px solid rgba(0,0,0,.1)}.iris-ie-lt9 .iris-square,.iris-ie-lt9 .iris-slider,.iris-ie-lt9 .iris-square-inner,.iris-ie-lt9 .iris-palette{outline:1px solid #aaa}.iris-ie-lt9 .iris-square .ui-slider-handle{outline:1px solid #aaa;background-color:#fff;-ms-filter:"alpha(Opacity=30)"}.iris-ie-lt9 .iris-square .iris-square-handle{background:0;border:3px solid #fff;-ms-filter:"alpha(Opacity=50)"}.iris-picker .iris-strip{margin-right:0;position:relative}.iris-picker .iris-strip .ui-slider-handle{position:absolute;background:0;margin:0;right:-3px;left:-3px;border:4px solid #aaa;border-width:4px 3px;width:auto;height:6px;border-radius:4px;box-shadow:0 1px 2px rgba(0,0,0,.2);opacity:.9;z-index:5;cursor:ns-resize}.iris-strip .ui-slider-handle:before{content:" ";position:absolute;left:-2px;right:-2px;top:-3px;bottom:-3px;border:2px solid #fff;border-radius:3px}.iris-picker .iris-slider-offset{position:absolute;top:11px;left:0;right:0;bottom:-3px;width:auto;height:auto;background:transparent;border:0;top:none;border-radius:0}.iris-picker .iris-square-handle{background:transparent;border:5px solid #aaa;border-radius:50%;border-color:rgba(128,128,128,.5);box-shadow:none;width:12px;height:12px;position:absolute;left:-10px;top:-10px;cursor:move;opacity:1;z-index:10}.iris-picker .ui-state-focus .iris-square-handle{opacity:.8}.iris-picker .iris-square-handle:hover{border-color:#999}.iris-picker .iris-square-value:focus .iris-square-handle{box-shadow:0 0 2px rgba(0,0,0,.75);opacity:.8}.iris-picker .iris-square-handle:hover::after{border-color:#fff}.iris-picker .iris-square-handle::after{position:absolute;bottom:-4px;right:-4px;left:-4px;top:-4px;border:3px solid #f9f9f9;border-color:rgba(255,255,255,.8);border-radius:50%;content:" "}.iris-picker .iris-square-value{width:8px;height:8px;position:absolute}.iris-ie-lt9 .iris-square-value,.iris-mozilla .iris-square-value{width:1px;height:1px}.iris-palette-container{position:absolute;bottom:0;left:0;margin:0;padding:0}.iris-border .iris-palette-container{left:10px;bottom:10px}.iris-picker .iris-palette{margin:0;cursor:pointer}';
// Bail for IE <= 7
if ( nonGradientIE && IEVersion <= 7 ) {
$.fn.iris = $.noop;
$.support.iris = false;
return;
}
$.support.iris = true;
function testGradientType() {
var el, base;
if ( nonGradientIE ) {
gradientType = 'filter';
}
else {
el = $( '<div id="iris-gradtest" />' );
base = 'linear-gradient(top,#fff,#000)';
$.each( vendorPrefixes, function( i, val ){
el.css( 'backgroundImage', val + base );
if ( el.css( 'backgroundImage').match( 'gradient' ) ) {
gradientType = i;
return false;
}
});
// check for legacy webkit gradient syntax
if ( gradientType === false ) {
el.css( 'background', '-webkit-gradient(linear,0% 0%,0% 100%,from(#fff),to(#000))' );
if ( el.css( 'backgroundImage').match( 'gradient' ) ) {
gradientType = 'webkit';
}
}
el.remove();
}
}
/**
* Only for CSS3 gradients. oldIE will use a separate function.
*
* Accepts as many color stops as necessary from 2nd arg on, or 2nd
* arg can be an array of color stops
*
* @param {string} origin Gradient origin - top or left, defaults to left.
* @return {string} Appropriate CSS3 gradient string for use in
*/
function createGradient( origin, stops ) {
origin = ( origin === 'top' ) ? 'top' : 'left';
stops = $.isArray( stops ) ? stops : Array.prototype.slice.call( arguments, 1 );
if ( gradientType === 'webkit' ) {
return legacyWebkitGradient( origin, stops );
} else {
return vendorPrefixes[ gradientType ] + 'linear-gradient(' + origin + ', ' + stops.join(', ') + ')';
}
}
/**
* Stupid gradients for a stupid browser.
*/
function stupidIEGradient( origin, stops ) {
var type, self, lastIndex, filter, startPosProp, endPosProp, dimensionProp, template, html;
origin = ( origin === 'top' ) ? 'top' : 'left';
stops = $.isArray( stops ) ? stops : Array.prototype.slice.call( arguments, 1 );
// 8 hex: AARRGGBB
// GradientType: 0 vertical, 1 horizontal
type = ( origin === 'top' ) ? 0 : 1;
self = $( this );
lastIndex = stops.length - 1;
filter = 'filter';
startPosProp = ( type === 1 ) ? 'left' : 'top';
endPosProp = ( type === 1 ) ? 'right' : 'bottom';
dimensionProp = ( type === 1 ) ? 'height' : 'width';
template = '<div class="iris-ie-gradient-shim" style="position:absolute;' + dimensionProp + ':100%;' + startPosProp + ':%start%;' + endPosProp + ':%end%;' + filter + ':%filter%;" data-color:"%color%"></div>';
html = '';
// need a positioning context
if ( self.css('position') === 'static' ) {
self.css( {position: 'relative' } );
}
stops = fillColorStops( stops );
$.each(stops, function( i, startColor ) {
var endColor, endStop, filterVal;
// we want two at a time. if we're on the last pair, bail.
if ( i === lastIndex ) {
return false;
}
endColor = stops[ i + 1 ];
//if our pairs are at the same color stop, moving along.
if ( startColor.stop === endColor.stop ) {
return;
}
endStop = 100 - parseFloat( endColor.stop ) + '%';
startColor.octoHex = new Color( startColor.color ).toIEOctoHex();
endColor.octoHex = new Color( endColor.color ).toIEOctoHex();
filterVal = 'progid:DXImageTransform.Microsoft.Gradient(GradientType=' + type + ', StartColorStr=\'' + startColor.octoHex + '\', EndColorStr=\'' + endColor.octoHex + '\')';
html += template.replace( '%start%', startColor.stop ).replace( '%end%', endStop ).replace( '%filter%', filterVal );
});
self.find( '.iris-ie-gradient-shim' ).remove();
$( html ).prependTo( self );
}
function legacyWebkitGradient( origin, colorList ) {
var stops = [];
origin = ( origin === 'top' ) ? '0% 0%,0% 100%,' : '0% 100%,100% 100%,';
colorList = fillColorStops( colorList );
$.each( colorList, function( i, val ){
stops.push( 'color-stop(' + ( parseFloat( val.stop ) / 100 ) + ', ' + val.color + ')' );
});
return '-webkit-gradient(linear,' + origin + stops.join(',') + ')';
}
function fillColorStops( colorList ) {
var colors = [],
percs = [],
newColorList = [],
lastIndex = colorList.length - 1;
$.each( colorList, function( index, val ) {
var color = val,
perc = false,
match = val.match( /1?[0-9]{1,2}%$/ );
if ( match ) {
color = val.replace( /\s?1?[0-9]{1,2}%$/, '' );
perc = match.shift();
}
colors.push( color );
percs.push( perc );
});
// back fill first and last
if ( percs[0] === false ) {
percs[0] = '0%';
}
if ( percs[lastIndex] === false ) {
percs[lastIndex] = '100%';
}
percs = backFillColorStops( percs );
$.each( percs, function( i ){
newColorList[i] = { color: colors[i], stop: percs[i] };
});
return newColorList;
}
function backFillColorStops( stops ) {
var first = 0,
last = stops.length - 1,
i = 0,
foundFirst = false,
incr,
steps,
step,
firstVal;
if ( stops.length <= 2 || $.inArray( false, stops ) < 0 ) {
return stops;
}
while ( i < stops.length - 1 ) {
if ( ! foundFirst && stops[i] === false ) {
first = i - 1;
foundFirst = true;
} else if ( foundFirst && stops[i] !== false ) {
last = i;
i = stops.length;
}
i++;
}
steps = last - first;
firstVal = parseInt( stops[first].replace('%'), 10 );
incr = ( parseFloat( stops[last].replace('%') ) - firstVal ) / steps;
i = first + 1;
step = 1;
while ( i < last ) {
stops[i] = ( firstVal + ( step * incr ) ) + '%';
step++;
i++;
}
return backFillColorStops( stops );
}
$.fn.gradient = function() {
var args = arguments;
return this.each( function() {
// this'll be oldishIE
if ( nonGradientIE ) {
stupidIEGradient.apply( this, args );
} else {
// new hotness
$( this ).css( 'backgroundImage', createGradient.apply( this, args ) );
}
});
};
$.fn.raninbowGradient = function( origin, args ) {
var opts, template, i, steps;
origin = origin || 'top';
opts = $.extend( {}, { s: 100, l: 50 }, args );
template = 'hsl(%h%,' + opts.s + '%,' + opts.l + '%)';
i = 0;
steps = [];
while ( i <= 360 ) {
steps.push( template.replace('%h%', i) );
i += 30;
}
return this.each(function() {
$(this).gradient( origin, steps );
});
};
// the colorpicker widget def.
Iris = {
options: {
color: false,
mode: 'hsl',
controls: {
horiz: 's', // horizontal defaults to saturation
vert: 'l', // vertical defaults to lightness
strip: 'h' // right strip defaults to hue
},
hide: true, // hide the color picker by default
border: true, // draw a border around the collection of UI elements
target: false, // a DOM element / jQuery selector that the element will be appended within. Only used when called on an input.
width: 200, // the width of the collection of UI elements
palettes: false // show a palette of basic colors beneath the square.
},
_color: '',
_palettes: [ '#000', '#fff', '#d33', '#d93', '#ee2', '#81d742', '#1e73be', '#8224e3' ],
_inited: false,
_defaultHSLControls: {
horiz: 's',
vert: 'l',
strip: 'h'
},
_defaultHSVControls: {
horiz: 'h',
vert: 'v',
strip: 's'
},
_scale: {
h: 360,
s: 100,
l: 100,
v: 100
},
_create: function() {
var self = this,
el = self.element,
color = self.options.color || el.val();
if ( gradientType === false ) {
testGradientType();
}
if ( el.is( 'input' ) ) {
if ( self.options.target ) {
self.picker = $( _html ).appendTo( self.options.target );
} else {
self.picker = $( _html ).insertAfter( el );
}
self._addInputListeners( el );
} else {
el.append( _html );
self.picker = el.find( '.iris-picker' );
}
// Browsers / Versions
// Feature detection doesn't work for these, and $.browser is deprecated
if ( isIE ) {
if ( IEVersion === 9 ) {
self.picker.addClass( 'iris-ie-9' );
} else if ( IEVersion <= 8 ) {
self.picker.addClass( 'iris-ie-lt9' );
}
} else if ( UA.indexOf('compatible') < 0 && UA.indexOf('khtml') < 0 && UA.match( /mozilla/ ) ) {
self.picker.addClass( 'iris-mozilla' );
}
if ( self.options.palettes ) {
self._addPalettes();
}
self._color = new Color( color ).setHSpace( self.options.mode );
self.options.color = self._color.toString();
// prep 'em for re-use
self.controls = {
square: self.picker.find( '.iris-square' ),
squareDrag: self.picker.find( '.iris-square-value' ),
horiz: self.picker.find( '.iris-square-horiz' ),
vert: self.picker.find( '.iris-square-vert' ),
strip: self.picker.find( '.iris-strip' ),
stripSlider: self.picker.find( '.iris-strip .iris-slider-offset' )
};
// small sanity check - if we chose hsv, change default controls away from hsl
if ( self.options.mode === 'hsv' && self._has('l', self.options.controls) ) {
self.options.controls = self._defaultHSVControls;
} else if ( self.options.mode === 'hsl' && self._has('v', self.options.controls) ) {
self.options.controls = self._defaultHSLControls;
}
// store it. HSL gets squirrely
self.hue = self._color.h();
if ( self.options.hide ) {
self.picker.hide();
}
if ( self.options.border ) {
self.picker.addClass( 'iris-border' );
}
self._initControls();
self.active = 'external';
self._dimensions();
self._change();
},
_has: function(needle, haystack) {
var ret = false;
$.each(haystack, function(i,v){
if ( needle === v ) {
ret = true;
// exit the loop
return false;
}
});
return ret;
},
_addPalettes: function () {
var container = $( '<div class="iris-palette-container" />' ),
palette = $( '<a class="iris-palette" tabindex="0" />' ),
colors = $.isArray( this.options.palettes ) ? this.options.palettes : this._palettes;
// do we have an existing container? Empty and reuse it.
if ( this.picker.find( '.iris-palette-container' ).length ) {
container = this.picker.find( '.iris-palette-container' ).detach().html( '' );
}
$.each(colors, function(index, val) {
palette.clone().data( 'color', val )
.css( 'backgroundColor', val ).appendTo( container )
.height( 10 ).width( 10 );
});
this.picker.append(container);
},
_paint: function() {
var self = this;
self._paintDimension( 'top', 'strip' );
self._paintDimension( 'top', 'vert' );
self._paintDimension( 'left', 'horiz' );
},
_paintDimension: function( origin, control ) {
var self = this,
c = self._color,
mode = self.options.mode,
color = self._getHSpaceColor(),
target = self.controls[ control ],
controlOpts = self.options.controls,
stops;
// don't paint the active control
if ( control === self.active || ( self.active === 'square' && control !== 'strip' ) ) {
return;
}
switch ( controlOpts[ control ] ) {
case 'h':
if ( mode === 'hsv' ) {
color = c.clone();
switch ( control ) {
case 'horiz':
color[controlOpts.vert](100);
break;
case 'vert':
color[controlOpts.horiz](100);
break;
case 'strip':
color.setHSpace('hsl');
break;
}
stops = color.toHsl();
} else {
if ( control === 'strip' ) {
stops = { s: color.s, l: color.l };
} else {
stops = { s: 100, l: color.l };
}
}
target.raninbowGradient( origin, stops );
break;
case 's':
if ( mode === 'hsv' ) {
if ( control === 'vert' ) {
stops = [ c.clone().a(0).s(0).toCSS('rgba'), c.clone().a(1).s(0).toCSS('rgba') ];
} else if ( control === 'strip' ) {
stops = [ c.clone().s(100).toCSS('hsl'), c.clone().s(0).toCSS('hsl') ];
} else if ( control === 'horiz' ) {
stops = [ '#fff', 'hsl(' + color.h + ',100%,50%)' ];
}
} else { // implicit mode === 'hsl'
if ( control === 'vert' && self.options.controls.horiz === 'h' ) {
stops = ['hsla(0, 0%, ' + color.l + '%, 0)', 'hsla(0, 0%, ' + color.l + '%, 1)'];
} else {
stops = ['hsl('+ color.h +',0%,50%)', 'hsl(' + color.h + ',100%,50%)'];
}
}
target.gradient( origin, stops );
break;
case 'l':
if ( control === 'strip' ) {
stops = ['hsl(' + color.h + ',100%,100%)', 'hsl(' + color.h + ', ' + color.s + '%,50%)', 'hsl('+ color.h +',100%,0%)'];
} else {
stops = ['#fff', 'rgba(255,255,255,0) 50%', 'rgba(0,0,0,0) 50%', 'rgba(0,0,0,1)'];
}
target.gradient( origin, stops );
break;
case 'v':
if ( control === 'strip' ) {
stops = [ c.clone().v(100).toCSS(), c.clone().v(0).toCSS() ];
} else {
stops = ['rgba(0,0,0,0)', '#000'];
}
target.gradient( origin, stops );
break;
default:
break;
}
},
_getHSpaceColor: function() {
return ( this.options.mode === 'hsv' ) ? this._color.toHsv() : this._color.toHsl();
},
_dimensions: function( reset ) {
// whatever size
var self = this,
opts = self.options,
controls = self.controls,
square = controls.square,
strip = self.picker.find( '.iris-strip' ),
squareWidth = '77.5%',
stripWidth = '12%',
totalPadding = 20,
innerWidth = opts.border ? opts.width - totalPadding : opts.width,
controlsHeight,
paletteCount = $.isArray( opts.palettes ) ? opts.palettes.length : self._palettes.length,
paletteMargin, paletteWidth, paletteContainerWidth;
if ( reset ) {
square.css( 'width', '' );
strip.css( 'width', '' );
self.picker.css( {width: '', height: ''} );
}
squareWidth = innerWidth * ( parseFloat( squareWidth ) / 100 );
stripWidth = innerWidth * ( parseFloat( stripWidth ) / 100 );
controlsHeight = opts.border ? squareWidth + totalPadding : squareWidth;
square.width( squareWidth ).height( squareWidth );
strip.height( squareWidth ).width( stripWidth );
self.picker.css( { width: opts.width, height: controlsHeight } );
if ( ! opts.palettes ) {
return self.picker.css( 'paddingBottom', '' );
}
// single margin at 2%
paletteMargin = squareWidth * 2 / 100;
paletteContainerWidth = squareWidth - ( ( paletteCount - 1 ) * paletteMargin );
paletteWidth = paletteContainerWidth / paletteCount;
self.picker.find('.iris-palette').each( function( i ) {
var margin = i === 0 ? 0 : paletteMargin;
$( this ).css({
width: paletteWidth,
height: paletteWidth,
marginLeft: margin
});
});
self.picker.css( 'paddingBottom', paletteWidth + paletteMargin );
strip.height( paletteWidth + paletteMargin + squareWidth );
},
_addInputListeners: function( input ) {
var self = this,
debounceTimeout = 100,
callback = function( event ){
var color = new Color( input.val() ),
val = input.val().replace( /^#/, '' );
input.removeClass( 'iris-error' );
// we gave a bad color
if ( color.error ) {
// don't error on an empty input - we want those allowed
if ( val !== '' ) {
input.addClass( 'iris-error' );
}
} else {
if ( color.toString() !== self._color.toString() ) {
// let's not do this on keyup for hex shortcodes
if ( ! ( event.type === 'keyup' && val.match( /^[0-9a-fA-F]{3}$/ ) ) ) {
self._setOption( 'color', color.toString() );
}
}
}
};
input.on( 'change', callback ).on( 'keyup', self._debounce( callback, debounceTimeout ) );
// If we initialized hidden, show on first focus. The rest is up to you.
if ( self.options.hide ) {
input.one( 'focus', function() {
self.show();
});
}
},
_initControls: function() {
var self = this,
controls = self.controls,
square = controls.square,
controlOpts = self.options.controls,
stripScale = self._scale[controlOpts.strip];
controls.stripSlider.slider({
orientation: 'vertical',
max: stripScale,
slide: function( event, ui ) {
self.active = 'strip';
// "reverse" for hue.
if ( controlOpts.strip === 'h' ) {
ui.value = stripScale - ui.value;
}
self._color[controlOpts.strip]( ui.value );
self._change.apply( self, arguments );
}
});
controls.squareDrag.draggable({
containment: 'parent',
zIndex: 1000,
cursor: 'move',
drag: function( event, ui ) {
self._squareDrag( event, ui );
},
start: function() {
square.addClass( 'iris-dragging' );
$(this).addClass( 'ui-state-focus' );
},
stop: function() {
square.removeClass( 'iris-dragging' );
$(this).removeClass( 'ui-state-focus' );
}
}).on( 'mousedown mouseup', function( event ) {
var focusClass = 'ui-state-focus';
event.preventDefault();
if (event.type === 'mousedown' ) {
self.picker.find( '.' + focusClass ).removeClass( focusClass ).blur();
$(this).addClass( focusClass ).focus();
} else {
$(this).removeClass( focusClass );
}
}).on( 'keydown', function( event ) {
var container = controls.square,
draggable = controls.squareDrag,
position = draggable.position(),
distance = self.options.width / 100; // Distance in pixels the draggable should be moved: 1 "stop"
// make alt key go "10"
if ( event.altKey ) {
distance *= 10;
}
// Reposition if one of the directional keys is pressed
switch ( event.keyCode ) {
case 37: position.left -= distance; break; // Left
case 38: position.top -= distance; break; // Up
case 39: position.left += distance; break; // Right
case 40: position.top += distance; break; // Down
default: return true; // Exit and bubble
}
// Keep draggable within container
position.left = Math.max( 0, Math.min( position.left, container.width() ) );
position.top = Math.max( 0, Math.min( position.top, container.height() ) );
draggable.css(position);
self._squareDrag( event, { position: position });
event.preventDefault();
});
// allow clicking on the square to move there and keep dragging
square.mousedown( function( event ) {
var squareOffset, pos;
// only left click
if ( event.which !== 1 ) {
return;
}
// prevent bubbling from the handle: no infinite loops
if ( ! $( event.target ).is( 'div' ) ) {
return;
}
squareOffset = self.controls.square.offset();
pos = {
top: event.pageY - squareOffset.top,
left: event.pageX - squareOffset.left
};
event.preventDefault();
self._squareDrag( event, { position: pos } );
event.target = self.controls.squareDrag.get(0);
self.controls.squareDrag.css( pos ).trigger( event );
});
// palettes
if ( self.options.palettes ) {
self._paletteListeners();
}
},
_paletteListeners: function() {
var self = this;
self.picker.find('.iris-palette-container').on('click.palette', '.iris-palette', function() {
self._color.fromCSS( $(this).data('color') );
self.active = 'external';
self._change();
}).on( 'keydown.palette', '.iris-palette', function( event ) {
if ( ! ( event.keyCode === 13 || event.keyCode === 32 ) ) {
return true;
}
event.stopPropagation();
$( this ).click();
});
},
_squareDrag: function( event, ui ) {
var self = this,
controlOpts = self.options.controls,
dimensions = self._squareDimensions(),
vertVal = Math.round( ( dimensions.h - ui.position.top ) / dimensions.h * self._scale[controlOpts.vert] ),
horizVal = self._scale[controlOpts.horiz] - Math.round( ( dimensions.w - ui.position.left ) / dimensions.w * self._scale[controlOpts.horiz] );
self._color[controlOpts.horiz]( horizVal )[controlOpts.vert]( vertVal );
self.active = 'square';
self._change.apply( self, arguments );
},
_setOption: function( key, value ) {
var self = this,
oldValue = self.options[key],
doDimensions = false,
hexLessColor,
newColor,
method;
// ensure the new value is set. We can reset to oldValue if some check wasn't met.
self.options[key] = value;
switch(key) {
case 'color':
// cast to string in case we have a number
value = '' + value;
hexLessColor = value.replace( /^#/, '' );
newColor = new Color( value ).setHSpace( self.options.mode );
if ( newColor.error ) {
self.options[key] = oldValue;
} else {
self._color = newColor;
self.options.color = self.options[key] = self._color.toString();
self.active = 'external';
self._change();
}
break;
case 'palettes':
doDimensions = true;
if ( value ) {
self._addPalettes();
} else {
self.picker.find('.iris-palette-container').remove();
}
// do we need to add events?
if ( ! oldValue ) {
self._paletteListeners();
}
break;
case 'width':
doDimensions = true;
break;
case 'border':
doDimensions = true;
method = value ? 'addClass' : 'removeClass';
self.picker[method]('iris-border');
break;
case 'mode':
case 'controls':
// if nothing's changed, let's bail, since this causes re-rendering the whole widget
if ( oldValue === value ) {
return;
}
// we're using these poorly named variables because they're already scoped.
// method is the element that Iris was called on. oldValue will be the options
method = self.element;
oldValue = self.options;
oldValue.hide = ! self.picker.is( ':visible' );
self.destroy();
self.picker.remove();
return $(self.element).iris(oldValue);
}
// Do we need to recalc dimensions?
if ( doDimensions ) {
self._dimensions(true);
}
},
_squareDimensions: function( forceRefresh ) {
var square = this.controls.square,
dimensions,
control;
if ( forceRefresh !== undef && square.data('dimensions') ) {
return square.data('dimensions');
}
control = this.controls.squareDrag;
dimensions = {
w: square.width(),
h: square.height()
};
square.data( 'dimensions', dimensions );
return dimensions;
},
_isNonHueControl: function( active, type ) {
if ( active === 'square' && this.options.controls.strip === 'h' ) {
return true;
} else if ( type === 'external' || ( type === 'h' && active === 'strip' ) ) {
return false;
}
return true;
},
_change: function() {
var self = this,
controls = self.controls,
color = self._getHSpaceColor(),
actions = [ 'square', 'strip' ],
controlOpts = self.options.controls,
type = controlOpts[self.active] || 'external',
oldHue = self.hue;
if ( self.active === 'strip' ) {
// take no action on any of the square sliders if we adjusted the strip
actions = [];
} else if ( self.active !== 'external' ) {
// for non-strip, non-external, strip should never change
actions.pop(); // conveniently the last item
}
$.each( actions, function(index, item) {
var value, dimensions, cssObj;
if ( item !== self.active ) {
switch ( item ) {
case 'strip':
// reverse for hue
value = ( controlOpts.strip === 'h' ) ? self._scale[controlOpts.strip] - color[controlOpts.strip] : color[controlOpts.strip];
controls.stripSlider.slider( 'value', value );
break;
case 'square':
dimensions = self._squareDimensions();
cssObj = {
left: color[controlOpts.horiz] / self._scale[controlOpts.horiz] * dimensions.w,
top: dimensions.h - ( color[controlOpts.vert] / self._scale[controlOpts.vert] * dimensions.h )
};
self.controls.squareDrag.css( cssObj );
break;
}
}
});
// Ensure that we don't change hue if we triggered a hue reset
if ( color.h !== oldHue && self._isNonHueControl( self.active, type ) ) {
self._color.h(oldHue);
}
// store hue for repeating above check next time
self.hue = self._color.h();
self.options.color = self._color.toString();
// only run after the first time
if ( self._inited ) {
self._trigger( 'change', { type: self.active }, { color: self._color } );
}
if ( self.element.is( ':input' ) && ! self._color.error ) {
self.element.removeClass( 'iris-error' );
if ( self.element.val() !== self._color.toString() ) {
self.element.val( self._color.toString() );
}
}
self._paint();
self._inited = true;
self.active = false;
},
// taken from underscore.js _.debounce method
_debounce: function( func, wait, immediate ) {
var timeout, result;
return function() {
var context = this,
args = arguments,
later,
callNow;
later = function() {
timeout = null;
if ( ! immediate) {
result = func.apply( context, args );
}
};
callNow = immediate && !timeout;
clearTimeout( timeout );
timeout = setTimeout( later, wait );
if ( callNow ) {
result = func.apply( context, args );
}
return result;
};
},
show: function() {
this.picker.show();
},
hide: function() {
this.picker.hide();
},
toggle: function() {
this.picker.toggle();
},
color: function(newColor) {
if ( newColor === true ) {
return this._color.clone();
} else if ( newColor === undef ) {
return this._color.toString();
}
this.option('color', newColor);
}
};
// initialize the widget
$.widget( 'a8c.iris', Iris );
// add CSS
$( '<style id="iris-css">' + _css + '</style>' ).appendTo( 'head' );
}( jQuery ));
/*! Color.js - v0.9.9 - 2012-11-20
* https://github.com/Automattic/Color.js
* Copyright (c) 2012 Matt Wiebe; Licensed GPL v2 */
(function(exports, undef) {
var Color = function( color, type ) {
if ( ! ( this instanceof Color ) )
return new Color( color, type );
return this._init( color, type );
};
Color.fn = Color.prototype = {
_color: 0,
_alpha: 1,
error: false,
// for preserving hue/sat in fromHsl().toHsl() flows
_hsl: { h: 0, s: 0, l: 0 },
// for preserving hue/sat in fromHsv().toHsv() flows
_hsv: { h: 0, s: 0, v: 0 },
// for setting hsl or hsv space - needed for .h() & .s() functions to function properly
_hSpace: 'hsl',
_init: function( color ) {
var func = 'noop';
switch ( typeof color ) {
case 'object':
// alpha?
if ( color.a !== undef )
this.a( color.a );
func = ( color.r !== undef ) ? 'fromRgb' :
( color.l !== undef ) ? 'fromHsl' :
( color.v !== undef ) ? 'fromHsv' : func;
return this[func]( color );
case 'string':
return this.fromCSS( color );
case 'number':
return this.fromInt( parseInt( color, 10 ) );
}
return this;
},
_error: function() {
this.error = true;
return this;
},
clone: function() {
var newColor = new Color( this.toInt() ),
copy = ['_alpha', '_hSpace', '_hsl', '_hsv', 'error'];
for ( var i = copy.length - 1; i >= 0; i-- ) {
newColor[ copy[i] ] = this[ copy[i] ];
}
return newColor;
},
setHSpace: function( space ) {
this._hSpace = ( space === 'hsv' ) ? space : 'hsl';
return this;
},
noop: function() {
return this;
},
fromCSS: function( color ) {
var nums, list,
leadingRE = /^(rgb|hs(l|v))a?\(/;
this.error = false;
// whitespace and semicolon trim
color = color.replace(/^\s+/, '').replace(/\s+$/, '').replace(/;$/, '');
if ( color.match(leadingRE) && color.match(/\)$/) ) {
list = color.replace(/(\s|%)/g, '').replace(leadingRE, '').replace(/,?\);?$/, '').split(',');
if ( list.length < 3 )
return this._error();
if ( list.length === 4 ) {
this.a( parseFloat( list.pop() ) );
// error state has been set to true in .a() if we passed NaN
if ( this.error )
return this;
}
for (var i = list.length - 1; i >= 0; i--) {
list[i] = parseInt(list[i], 10);
if ( isNaN( list[i] ) )
return this._error();
}
if ( color.match(/^rgb/) ) {
return this.fromRgb( {
r: list[0],
g: list[1],
b: list[2]
} );
} else if ( color.match(/^hsv/) ) {
return this.fromHsv( {
h: list[0],
s: list[1],
v: list[2]
} );
} else {
return this.fromHsl( {
h: list[0],
s: list[1],
l: list[2]
} );
}
} else {
// must be hex amirite?
return this.fromHex( color );
}
},
fromRgb: function( rgb, preserve ) {
if ( typeof rgb !== 'object' || rgb.r === undef || rgb.g === undef || rgb.b === undef )
return this._error();
this.error = false;
return this.fromInt( parseInt( ( rgb.r << 16 ) + ( rgb.g << 8 ) + rgb.b, 10 ), preserve );
},
fromHex: function( color ) {
color = color.replace(/^#/, '').replace(/^0x/, '');
if ( color.length === 3 ) {
color = color[0] + color[0] + color[1] + color[1] + color[2] + color[2];
}
// rough error checking - this is where things go squirrely the most
this.error = ! /^[0-9A-F]{6}$/i.test( color );
return this.fromInt( parseInt( color, 16 ) );
},
fromHsl: function( hsl ) {
var r, g, b, q, p, h, s, l;
if ( typeof hsl !== 'object' || hsl.h === undef || hsl.s === undef || hsl.l === undef )
return this._error();
this._hsl = hsl; // store it
this._hSpace = 'hsl'; // implicit
h = hsl.h / 360; s = hsl.s / 100; l = hsl.l / 100;
if ( s === 0 ) {
r = g = b = l; // achromatic
}
else {
q = l < 0.5 ? l * ( 1 + s ) : l + s - l * s;
p = 2 * l - q;
r = this.hue2rgb( p, q, h + 1/3 );
g = this.hue2rgb( p, q, h );
b = this.hue2rgb( p, q, h - 1/3 );
}
return this.fromRgb( {
r: r * 255,
g: g * 255,
b: b * 255
}, true ); // true preserves hue/sat
},
fromHsv: function( hsv ) {
var h, s, v, r, g, b, i, f, p, q, t;
if ( typeof hsv !== 'object' || hsv.h === undef || hsv.s === undef || hsv.v === undef )
return this._error();
this._hsv = hsv; // store it
this._hSpace = 'hsv'; // implicit
h = hsv.h / 360; s = hsv.s / 100; v = hsv.v / 100;
i = Math.floor( h * 6 );
f = h * 6 - i;
p = v * ( 1 - s );
q = v * ( 1 - f * s );
t = v * ( 1 - ( 1 - f ) * s );
switch( i % 6 ) {
case 0:
r = v; g = t; b = p;
break;
case 1:
r = q; g = v; b = p;
break;
case 2:
r = p; g = v; b = t;
break;
case 3:
r = p; g = q; b = v;
break;
case 4:
r = t; g = p; b = v;
break;
case 5:
r = v; g = p; b = q;
break;
}
return this.fromRgb( {
r: r * 255,
g: g * 255,
b: b * 255
}, true ); // true preserves hue/sat
},
// everything comes down to fromInt
fromInt: function( color, preserve ) {
this._color = parseInt( color, 10 );
if ( isNaN( this._color ) )
this._color = 0;
// let's coerce things
if ( this._color > 16777215 )
this._color = 16777215;
else if ( this._color < 0 )
this._color = 0;
// let's not do weird things
if ( preserve === undef ) {
this._hsv.h = this._hsv.s = this._hsl.h = this._hsl.s = 0;
}
// EVENT GOES HERE
return this;
},
hue2rgb: function( p, q, t ) {
if ( t < 0 ) {
t += 1;
}
if ( t > 1 ) {
t -= 1;
}
if ( t < 1/6 ) {
return p + ( q - p ) * 6 * t;
}
if ( t < 1/2 ) {
return q;
}
if ( t < 2/3 ) {
return p + ( q - p ) * ( 2/3 - t ) * 6;
}
return p;
},
toString: function() {
var hex = parseInt( this._color, 10 ).toString( 16 );
if ( this.error )
return '';
// maybe left pad it
if ( hex.length < 6 ) {
for (var i = 6 - hex.length - 1; i >= 0; i--) {
hex = '0' + hex;
}
}
return '#' + hex;
},
toCSS: function( type, alpha ) {
type = type || 'hex';
alpha = parseFloat( alpha || this._alpha );
switch ( type ) {
case 'rgb':
case 'rgba':
var rgb = this.toRgb();
if ( alpha < 1 ) {
return "rgba( " + rgb.r + ", " + rgb.g + ", " + rgb.b + ", " + alpha + " )";
}
else {
return "rgb( " + rgb.r + ", " + rgb.g + ", " + rgb.b + " )";
}
break;
case 'hsl':
case 'hsla':
var hsl = this.toHsl();
if ( alpha < 1 ) {
return "hsla( " + hsl.h + ", " + hsl.s + "%, " + hsl.l + "%, " + alpha + " )";
}
else {
return "hsl( " + hsl.h + ", " + hsl.s + "%, " + hsl.l + "% )";
}
break;
default:
return this.toString();
}
},
toRgb: function() {
return {
r: 255 & ( this._color >> 16 ),
g: 255 & ( this._color >> 8 ),
b: 255 & ( this._color )
};
},
toHsl: function() {
var rgb = this.toRgb();
var r = rgb.r / 255, g = rgb.g / 255, b = rgb.b / 255;
var max = Math.max( r, g, b ), min = Math.min( r, g, b );
var h, s, l = ( max + min ) / 2;
if ( max === min ) {
h = s = 0; // achromatic
} else {
var d = max - min;
s = l > 0.5 ? d / ( 2 - max - min ) : d / ( max + min );
switch ( max ) {
case r: h = ( g - b ) / d + ( g < b ? 6 : 0 );
break;
case g: h = ( b - r ) / d + 2;
break;
case b: h = ( r - g ) / d + 4;
break;
}
h /= 6;
}
// maintain hue & sat if we've been manipulating things in the HSL space.
h = Math.round( h * 360 );
if ( h === 0 && this._hsl.h !== h ) {
h = this._hsl.h;
}
s = Math.round( s * 100 );
if ( s === 0 && this._hsl.s ) {
s = this._hsl.s;
}
return {
h: h,
s: s,
l: Math.round( l * 100 )
};
},
toHsv: function() {
var rgb = this.toRgb();
var r = rgb.r / 255, g = rgb.g / 255, b = rgb.b / 255;
var max = Math.max( r, g, b ), min = Math.min( r, g, b );
var h, s, v = max;
var d = max - min;
s = max === 0 ? 0 : d / max;
if ( max === min ) {
h = s = 0; // achromatic
} else {
switch( max ){
case r:
h = ( g - b ) / d + ( g < b ? 6 : 0 );
break;
case g:
h = ( b - r ) / d + 2;
break;
case b:
h = ( r - g ) / d + 4;
break;
}
h /= 6;
}
// maintain hue & sat if we've been manipulating things in the HSV space.
h = Math.round( h * 360 );
if ( h === 0 && this._hsv.h !== h ) {
h = this._hsv.h;
}
s = Math.round( s * 100 );
if ( s === 0 && this._hsv.s ) {
s = this._hsv.s;
}
return {
h: h,
s: s,
v: Math.round( v * 100 )
};
},
toInt: function() {
return this._color;
},
toIEOctoHex: function() {
// AARRBBGG
var hex = this.toString();
var AA = parseInt( 255 * this._alpha, 10 ).toString(16);
if ( AA.length === 1 ) {
AA = '0' + AA;
}
return '#' + AA + hex.replace(/^#/, '' );
},
toLuminosity: function() {
var rgb = this.toRgb();
return 0.2126 * Math.pow( rgb.r / 255, 2.2 ) + 0.7152 * Math.pow( rgb.g / 255, 2.2 ) + 0.0722 * Math.pow( rgb.b / 255, 2.2);
},
getDistanceLuminosityFrom: function( color ) {
if ( ! ( color instanceof Color ) ) {
throw 'getDistanceLuminosityFrom requires a Color object';
}
var lum1 = this.toLuminosity();
var lum2 = color.toLuminosity();
if ( lum1 > lum2 ) {
return ( lum1 + 0.05 ) / ( lum2 + 0.05 );
}
else {
return ( lum2 + 0.05 ) / ( lum1 + 0.05 );
}
},
getMaxContrastColor: function() {
var lum = this.toLuminosity();
var hex = ( lum >= 0.5 ) ? '000000' : 'ffffff';
return new Color( hex );
},
getGrayscaleContrastingColor: function( contrast ) {
if ( ! contrast ) {
return this.getMaxContrastColor();
}
// don't allow less than 5
var target_contrast = ( contrast < 5 ) ? 5 : contrast;
var color = this.getMaxContrastColor();
contrast = color.getDistanceLuminosityFrom( this );
// if current max contrast is less than the target contrast, we had wishful thinking.
if ( contrast <= target_contrast ) {
return color;
}
var incr = ( 0 === color.toInt() ) ? 1 : -1;
while ( contrast > target_contrast ) {
color = color.incrementLightness( incr );
contrast = color.getDistanceLuminosityFrom( this );
}
return color;
},
getReadableContrastingColor: function( bgColor, minContrast ) {
if ( ! bgColor instanceof Color ) {
return this;
}
// you shouldn't use less than 5, but you might want to.
var targetContrast = ( minContrast === undef ) ? 5 : minContrast;
// working things
var contrast = bgColor.getDistanceLuminosityFrom( this );
var maxContrastColor = bgColor.getMaxContrastColor();
var maxContrast = maxContrastColor.getDistanceLuminosityFrom( bgColor );
// if current max contrast is less than the target contrast, we had wishful thinking.
// still, go max
if ( maxContrast <= targetContrast ) {
return maxContrastColor;
}
// or, we might already have sufficient contrast
else if ( contrast >= targetContrast ) {
return this;
}
var incr = ( 0 === maxContrastColor.toInt() ) ? -1 : 1;
while ( contrast < targetContrast ) {
this.incrementLightness( incr );
contrast = this.getDistanceLuminosityFrom( bgColor );
// infininite loop prevention: you never know.
if ( this._color === 0 || this._color === 16777215 ) {
break;
}
}
return this;
},
a: function( val ) {
if ( val === undef )
return this._alpha;
var a = parseFloat( val );
if ( isNaN( a ) )
return this._error();
this._alpha = a;
return this;
},
// TRANSFORMS
darken: function( amount ) {
amount = amount || 5;
return this.l( - amount, true );
},
lighten: function( amount ) {
amount = amount || 5;
return this.l( amount, true );
},
saturate: function( amount ) {
amount = amount || 15;
return this.s( amount, true );
},
desaturate: function( amount ) {
amount = amount || 15;
return this.s( - amount, true );
},
toGrayscale: function() {
return this.setHSpace('hsl').s( 0 );
},
getComplement: function() {
return this.h( 180, true );
},
getSplitComplement: function( step ) {
step = step || 1;
var incr = 180 + ( step * 30 );
return this.h( incr, true );
},
getAnalog: function( step ) {
step = step || 1;
var incr = step * 30;
return this.h( incr, true );
},
getTetrad: function( step ) {
step = step || 1;
var incr = step * 60;
return this.h( incr, true );
},
getTriad: function( step ) {
step = step || 1;
var incr = step * 120;
return this.h( incr, true );
},
_partial: function( key ) {
var prop = shortProps[key];
return function( val, incr ) {
var color = this._spaceFunc('to', prop.space);
// GETTER
if ( val === undef )
return color[key];
// INCREMENT
if ( incr === true )
val = color[key] + val;
// MOD & RANGE
if ( prop.mod )
val = val % prop.mod;
if ( prop.range )
val = ( val < prop.range[0] ) ? prop.range[0] : ( val > prop.range[1] ) ? prop.range[1] : val;
// NEW VALUE
color[key] = val;
return this._spaceFunc('from', prop.space, color);
};
},
_spaceFunc: function( dir, s, val ) {
var space = s || this._hSpace,
funcName = dir + space.charAt(0).toUpperCase() + space.substr(1);
return this[funcName](val);
}
};
var shortProps = {
h: {
mod: 360
},
s: {
range: [0,100]
},
l: {
space: 'hsl',
range: [0,100]
},
v: {
space: 'hsv',
range: [0,100]
},
r: {
space: 'rgb',
range: [0,255]
},
g: {
space: 'rgb',
range: [0,255]
},
b: {
space: 'rgb',
range: [0,255]
}
};
for ( var key in shortProps ) {
if ( shortProps.hasOwnProperty( key ) )
Color.fn[key] = Color.fn._partial(key);
}
exports.Color = Color;
}(typeof exports === 'object' && exports || this));
|