-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolorhack.js
More file actions
2041 lines (1810 loc) · 85.4 KB
/
colorhack.js
File metadata and controls
2041 lines (1810 loc) · 85.4 KB
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
/*
================================
GLOBAL VARIABLES
================================
*/
/* Settings */
// Safe to change.
// Modify these values to resolve conflicts with the page's elements.
var CH_PREFIX = 'colorhack_'; // HTML id prefix for naming ColorHack elements
var CH_CLASS = 'colorhack'; // HTML class to identify ColorHack elements
var BASE_Z_INDEX = 9000; // minimum z-index of ColorHack elements
var DIALOG_COLOR_SCHEMES_HEIGHT = 400; // color-schemes dialog height
var DIALOG_COLOR_SCHEMES_WIDTH = 220; // color-schemes dialog width
var DIALOG_COLOR_SETTINGS_HEIGHT = 'auto'; // color-settings dialog height
var DIALOG_COLOR_SETTINGS_WIDTH = 352; // color-settings dialog width
var DIALOG_COLORSCHEME_DETAILS_HEIGHT = 300; // colorscheme-details dialog height
var DIALOG_COLORSCHEME_DETAILS_WIDTH = 300; // colorscheme-details dialog width
var SLIDE_DURATION = 300; // time taken to complete slide animations
var HIGHLIGHT_BORDER_COLOR = 'rgb(50, 200, 200)' // border color used to highlight elements in add member mode
var HIGHLIGHT_BOX_SHADOW_COLOR = 'rgb(80, 120, 255)' // box shadow color used to highlight elements in add member mode
/* Constants used by ColorHack */
// Not safe to change.
// Type enumeration of color scheme color types (used in color schemes)
var COLOR_TYPE_FOREGROUND = 1;
var COLOR_TYPE_BACKGROUND = 2;
var COLOR_TYPE_BORDER = 3;
// ColorHack object
var COLORHACK;
/*
================================
COLORHACK CLASS
================================
* Contains properties and methods for ColorHack objects.
* Any page should only have one instance of the ColorHack class.
*/
function ColorHack() {
/*
================================
PREDECLARE VARIABLES
================================
*/
// Private properties
var _dialogs = [];
var _menu = {};
var _toolbar = {};
var _icon = {};
var _colorSchemes = {};
var _colorSettings = {};
var _colorSettingsColors = {};
var _colorschemeDetails = {};
// Public properties
this.components = {};
this.colorSchemes = [];
// Private methods
var _GetElPos = function() {}
var _RgbToHex = function() {}
var _HexToRgb = function() {}
var _CreateDefaultColorScheme = function() {}
var _CreateColorSchemeMember = function() {}
var _AddColorSchemeMembers = function() {}
var _AddColorSchemes = function() {}
var _UpdateColorSchemes = function() {}
var _ToggleColorScheme = function() {}
var _SelectColorScheme = function() {}
var _RemoveColorScheme = function() {}
var _RemoveColorSchemeMember = function() {}
var _FillGradient = function() {}
var _SetActiveColor = function() {}
var _UpdateActiveColor = function() {}
var _SubmitColorTextbox = function() {}
// Public methods
this.BuildDialogs = function() {}
this.SetupColorPickers = function() {}
this.AttachEvents = function() {}
this.SetDefaults = function() {}
this.Init = function() {}
/*
================================
CREATE-COMPONENT METHODS
================================
*/
// Creates dialog header elements.
var _CreateDialogHeader = function(title) {
var header =
$('<h2/>', {
'class': CH_CLASS + ' ' +
CH_PREFIX + 'dialog-header'
})
.append( // dialog title
$('<span/>', {
'class': CH_CLASS + ' ' +
CH_PREFIX + 'dialog-title'
})
.text(title)
)
.append( // dialog close button
$('<span/>', {
'class': CH_CLASS + ' ' +
CH_PREFIX + 'dialog-close'
})
.html('×')
)
;
return header;
}
// Creates color picker elements for a single color.
var _CreateColorPicker = function(color) {
var colorPicker =
$('<div/>', { // container for color picker elements
id: CH_PREFIX + 'color-settings_color-' + color,
'class': CH_CLASS + ' ' +
CH_PREFIX + 'color-settings_color',
})
.append(
$('<label/>', { // label for color picker textbox - R:/G:/B:
id: CH_PREFIX + 'color-settings_color-label-' + color,
'class': CH_CLASS + ' ' +
CH_PREFIX + 'color-label',
'for': CH_PREFIX + 'color-settings_color-textbox-' + color
})
.html(color.substring(0, 1).toUpperCase() + ':')
)
.append(
$('<div/>', { // color picker
id: CH_PREFIX + 'color-settings_color-picker-' + color,
'class': CH_CLASS + ' ' +
CH_PREFIX + 'color-picker',
})
.append(
$('<canvas/>', { // color picker gradient
id: CH_PREFIX + 'color-settings_color-picker_gradient-' + color,
'class': CH_CLASS + ' ' +
CH_PREFIX + 'color-picker_gradient',
'Height': '24px', // WARNING: This is a hack, since this normally it changes
'Width': '256px' // the CSS height/width and not the element's height/width.
})
.append(
$('<p/>').html('Your browser does not support this feature')
)
.data('color', color)
.data('selector', '#' + CH_PREFIX + 'color-settings_color-picker_selector-' + color)
)
.append(
$('<div/>', { // color picker selector
id: CH_PREFIX + 'color-settings_color-picker_selector-' + color,
'class': CH_CLASS + ' ' +
CH_PREFIX + 'color-picker_selector'
})
.data('color', color)
.data('gradient', '#' + CH_PREFIX + 'color-settings_color-picker_gradient-' + color)
)
.data('color', color)
)
.append(
$('<input/>', { // textbox for color picker
id: CH_PREFIX + 'color-settings_color-textbox-' + color,
'class': CH_CLASS + ' ' +
CH_PREFIX + 'color-textbox',
name: CH_PREFIX + 'color-textbox-' + color,
type: 'text',
})
.data('color', color)
.data('selector', '#' + CH_PREFIX + 'color-settings_color-picker_selector-' + color)
)
return colorPicker;
}
/*
================================
PRIVATE PROPERTIES
================================
*/
// Counts the total number of color schemes created (included deleted ones).
// Used to assign IDs to newly-created color schemes.
var schemeCount = 0;
// Variables used to count number of clicks.
// Used to differentiate between single, double, etc. clicks.
var _clickTimer = null, _clicks = 0;
// Regex variables for input validation and filtering.
var regexAlpha = /^[a-zA-Z]*$/;
var regexAlphaNum = /^[a-zA-Z0-9]*$/;
var regexDec = /^[0-9]*$/;
var regexHex = /^[a-fA-F0-9]*$/;
// Boolean to track whether the control/command key is pressed.
var _isCtrlPressed = false;
// Indicates whether we are adding an element to the active color scheme.
var _inAddMemberMode = false;
// Dialog element ID of the member whose add button was clicked to enter add member mode
// (used to determine where to add the new member).
var _addMemberModeOriginator = null;
// Dialog element ID of the color scheme we are adding a member to (while in add member mode).
var _addMemberModeSchemeId = null;
// Object to hold original element styling when temporarily changing them in add color scheme member mode.
var _originalStyling = {};
// Dialogs that make up the main UI.
// Used to generate the menu toolbar options.
var _dialogs = [
{ id: 'color-schemes', name: 'Color Schemes' },
{ id: 'color-settings', name: 'Color Settings' },
{ id: 'colorscheme-details', name: 'Color Scheme Details' }
];
/*
--------------------------------
MENU
--------------------------------
*/
// Menu located on the bottom-right of the page.
// Contains the icon and toolbar components.
var _menu =
$('<div/>', {
id: CH_PREFIX + 'menu',
'class': CH_CLASS
})
;
// Toolbar located on the bottom-right of the page.
// Provides a menu to toggle visibility of ColorHack dialogs.
var _toolbar =
$('<ul/>', {
id: CH_PREFIX + 'toolbar',
'class': CH_CLASS
})
// Add toolbar options.
.append(
// Create toolbar items.
(function() {
var toolbarOptions = $(); // empty jQuery object
// Build collection of toolbar options from dialogs array
for (var i = 0 ; i < _dialogs.length ; i++) {
toolbarOptions = toolbarOptions.add(
$('<li/>', {
id: CH_PREFIX + 'toolbar_' + _dialogs[i].id,
'class': CH_CLASS + ' ' +
CH_PREFIX + 'toolbar-item'
})
.append( // checkmark element
$('<span/>', {
'class': CH_CLASS + ' ' +
CH_PREFIX + 'toolbar-item-check'
})
.html('✓') // checkmark
)
.append(_dialogs[i].name)
);
}
return toolbarOptions;
})()
)
.hide()
.appendTo(_menu)
;
// Menu icon located on the bottom-right of the page.
// Toggles visibility of the toolbar.
var _icon =
$('<h1/>', {
id: CH_PREFIX + 'icon',
'class': CH_CLASS
})
.text('ColorHack')
.appendTo(_menu)
;
/*
--------------------------------
COLOR SCHEMES DIALOG
--------------------------------
*/
// Color scheme dialog.
// Manages color rules and elements that use the rules.
var _colorSchemes =
$('<div/>', {
id: CH_PREFIX + 'color-schemes',
'class': CH_CLASS + ' ' +
CH_PREFIX + 'dialog'
})
// Add dialog header.
.append(
_CreateDialogHeader('Color Schemes')
)
.append(
$('<div/>', {
id: CH_PREFIX + 'color-schemes_schemes',
'class': CH_CLASS + ' ' +
CH_PREFIX + 'color-schemes'
})
)
;
/*
--------------------------------
COLOR SETTINGS DIALOG
--------------------------------
*/
// Color settings dialog.
// Manages the set of color styles in a color rule.
var _colorSettings =
$('<div/>', {
id: CH_PREFIX + 'color-settings',
'class': CH_CLASS + ' ' +
CH_PREFIX + 'dialog'
})
// Add dialog header.
.append(
_CreateDialogHeader('Color Settings')
)
;
// Color picker component of color settings dialog.
// Uses sliders to control R, G, B, and A levels.
// Also contains text fields for manually entering values.
var _colorSettingsColors =
$('<div/>', {
id: CH_PREFIX + 'color-settings_colors',
'class': CH_CLASS,
})
.append(
$('<div/>', {
id: CH_PREFIX + 'color-settings_color-hex',
'class': CH_CLASS + ' ' +
CH_PREFIX + 'color-settings_color-hex'
})
.append(
$('<label/>', {
id: CH_PREFIX + 'color-settings_color-label-hex',
'class': CH_CLASS + ' ' +
CH_PREFIX + 'color-label',
'for': CH_PREFIX + 'color-settings_color-textbox-hex'
})
.html('#:')
)
.append(
$('<div/>', {
id: CH_PREFIX + 'color-settings_color-components',
'class': CH_CLASS + ' ' +
CH_PREFIX + 'color-components'
})
.append(
(function() {
var components = $();
var colors = ['red', 'green', 'blue'];
for (var i = 0; i < colors.length; i++) {
components = components.add(
$('<div/>', {
id: CH_PREFIX + 'color-settings_color-component-' + colors[i],
'class': CH_CLASS + ' ' +
CH_PREFIX + 'color-component'
})
)
}
return components;
})()
)
.append(
$('<div/>', {
id: CH_PREFIX + 'color-settings_color-full',
'class': CH_CLASS + ' ' +
CH_PREFIX + 'color-full'
})
)
)
.append(
$('<input/>', {
id: CH_PREFIX + 'color-settings_color-textbox-hex',
'class': CH_CLASS + ' ' +
CH_PREFIX + 'color-textbox-hex',
name: CH_PREFIX + 'color-textbox-hex',
type: 'text'
})
)
)
.append(
_CreateColorPicker('red')
)
.append(
_CreateColorPicker('green')
)
.append(
_CreateColorPicker('blue')
)
.append(
_CreateColorPicker('alpha')
)
.appendTo(_colorSettings)
;
/*
--------------------------------
COLORSCHEME DETAILS DIALOG
--------------------------------
*/
// Color scheme details dialog.
// Shows details on element attributes and applied color rules.
var _colorschemeDetails =
$('<div/>', {
id: CH_PREFIX + 'colorscheme-details',
'class': CH_CLASS + ' ' +
CH_PREFIX + 'dialog'
})
// Add dialog header
.append(
_CreateDialogHeader('Color Scheme Details')
)
;
/*
================================
PUBLIC PROPERTIES
================================
*/
// Primary UI components used by ColorHack.
this.components = {
menu: _menu,
icon: _icon,
toolbar: _toolbar,
dialogs: $()
.add(_colorSchemes)
.add(_colorSettings)
.add(_colorschemeDetails),
'color-schemes': _colorSchemes,
'color-schemes_schemes': _colorSchemes.find('.' + CH_PREFIX + 'color-schemes'),
'color-settings': _colorSettings,
'color-settings_colors': _colorSettingsColors,
'color-components': {
red: _colorSettingsColors.find('#' + CH_PREFIX + 'color-settings_color-component-red'),
green: _colorSettingsColors.find('#' + CH_PREFIX + 'color-settings_color-component-green'),
blue: _colorSettingsColors.find('#' + CH_PREFIX + 'color-settings_color-component-blue'),
full: _colorSettingsColors.find('#' + CH_PREFIX + 'color-settings_color-full')
},
'color-pickers': {
red: _colorSettingsColors.find('#' + CH_PREFIX + 'color-settings_color-picker-red'),
green: _colorSettingsColors.find('#' + CH_PREFIX + 'color-settings_color-picker-green'),
blue: _colorSettingsColors.find('#' + CH_PREFIX + 'color-settings_color-picker-blue'),
alpha: _colorSettingsColors.find('#' + CH_PREFIX + 'color-settings_color-picker-alpha')
},
'color-picker_gradients': {
red: _colorSettingsColors.find('#' + CH_PREFIX + 'color-settings_color-picker_gradient-red'),
green: _colorSettingsColors.find('#' + CH_PREFIX + 'color-settings_color-picker_gradient-green'),
blue: _colorSettingsColors.find('#' + CH_PREFIX + 'color-settings_color-picker_gradient-blue'),
alpha: _colorSettingsColors.find('#' + CH_PREFIX + 'color-settings_color-picker_gradient-alpha')
},
'color-textboxes': {
hex: _colorSettingsColors.find('#' + CH_PREFIX + 'color-settings_color-textbox-hex'),
red: _colorSettingsColors.find('#' + CH_PREFIX + 'color-settings_color-textbox-red'),
green: _colorSettingsColors.find('#' + CH_PREFIX + 'color-settings_color-textbox-green'),
blue: _colorSettingsColors.find('#' + CH_PREFIX + 'color-settings_color-textbox-blue'),
alpha: _colorSettingsColors.find('#' + CH_PREFIX + 'color-settings_color-textbox-alpha')
},
'colorscheme-details': _colorschemeDetails
};
// Contains all the color schemes tracked by ColorHack.
// Each color scheme is an object of the following form:
// {
// id: string // id of color scheme element in color schemes dialog
// name: string // arbitrary non-unique name given to color scheme
// memberCount: integer // similar purpose to schemeCount for COLORHACK object
// members: [ { // array of objects to keep track of elements belonging to color scheme
// id: string // id of member element in color schemes dialog
// name: string // arbitrary non-unique name
// el: element // DOM element
// }, ... ]
// colors: [ { // array of objects describing color properties of color scheme
// type: integer // enumeration, describes type of color (e.g. color, background, ...)
// value: { r: integer, g: integer, b: integer } // rgb object
// }, ... ]
// }
this.colorSchemes = [];
// Should I even keep track of color schemes interally? Is it necessary?
// Adding and deleting schemes and scheme members would be simpler without this...
// If I need to do something with the color schemes later though (like generate CSS),
// I would rather not go through the DOM to do it...
/*
================================
PRIVATE METHODS
================================
*/
// Returns the position of an element on the page.
var _GetElPos = function(el) {
for (
var xPos = 0, yPos = 0;
el != null;
xPos += el.offsetLeft, yPos += el.offsetTop, el = el.offsetParent
);
return {x: xPos, y: yPos};
}
// Converts RGB color as { r: x, g: y, b: z } to hex color as '#abcdef'.
var _RgbToHex = function(rgb) {
return '' +
((1 << 24) +
(+rgb.red << 16) +
(+rgb.green << 8) +
+rgb.blue)
.toString(16).slice(1).toUpperCase();
}
// Converts hex color as '#abcdef' to RGB color as { r: x, g: y, b: z }.
var _HexToRgb = function(hex) {
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16),
a: 0
} : null;
}
// Creates and returns a default internal color scheme object.
var _CreateDefaultColorScheme = function() {
var cs = {
id: CH_PREFIX + 'scheme' + schemeCount,
name: 'Color Scheme ' + (schemeCount+1),
memberCount: 0,
members: [],
colors: [
{ type: COLOR_TYPE_FOREGROUND,
value: { r: 0, g: 0, b: 0 } },
{ type: COLOR_TYPE_BACKGROUND,
value: { r: 255, g: 255, b: 255 } },
{ type: COLOR_TYPE_BORDER,
value: { r: 0, g: 0, b: 0 } }
]
};
schemeCount++;
return cs;
}
// Creates and returns an internal color scheme member object.
var _CreateColorSchemeMember = function(element, elementName, schemeIndex, scheme) {
var member = {
id: CH_PREFIX + 'scheme' + schemeIndex + '_member' + scheme.memberCount,
name: elementName,
el: element
}
scheme.memberCount++;
return member;
}
// Adds one or more members to the input color scheme.
// Input color scheme is a JQ DOM element, members is an array of member objects.
var _AddColorSchemeMembers = function($scheme, members, afterTarget) {
var $newMembers = $();
var addColorSchemeMember = function(index) {
$newMembers = $newMembers.add(
$('<div/>', {
id: members[index].id,
'class': CH_CLASS + ' ' +
CH_PREFIX + 'color-scheme_member'
})
.append(
$('<span/>', {
'class': CH_CLASS + ' ' +
CH_PREFIX + 'color-scheme_member_name'
})
.html( // shorten displayed name if too long to fit
members[index].name.length > 20 ?
members[index].name.substring(0, 20) + '...' :
members[index].name
)
)
.append(
$('<span/>', {
'class': CH_CLASS + ' ' +
CH_PREFIX + 'inline-button ' +
CH_PREFIX + 'small ' +
CH_PREFIX + 'color-scheme_add'
})
.html('<span>+</span>')
)
.append(
$('<span/>', {
'class': CH_CLASS + ' ' +
CH_PREFIX + 'inline-button ' +
CH_PREFIX + 'small ' +
CH_PREFIX + 'color-scheme_remove'
})
.html('<span>−</span>')
)
);
}
for (var i = 0; i < members.length; i++) {
addColorSchemeMember(i);
}
// Add color scheme members to DOM.
if (typeof afterTarget === 'undefined') {
$scheme.find('.' + CH_PREFIX + 'color-scheme_members').append($newMembers);
} else {
$newMembers.insertAfter(afterTarget);
}
}
// Adds one or more color schemes to Color Schemes dialog.
// Input color schemes is an array of color scheme objects.
var _AddColorSchemes = function(schemes, afterTarget) {
var $newSchemes = $();
var addColorScheme = function(index) {
$newSchemes = $newSchemes.add(
$('<div/>', {
id: schemes[index].id,
'class': CH_CLASS + ' ' +
CH_PREFIX + 'color-scheme'
})
.append(
$('<h3/>', {
'class': CH_CLASS + ' ' +
CH_PREFIX + 'color-scheme_title'
})
.append(
$('<span/>', {
'class': CH_CLASS + ' ' +
CH_PREFIX + 'inline-button ' +
CH_PREFIX + 'color-scheme_toggle'
})
// #x25b6 = black right triangle arrow
// #x25bc = black down triangle arrow
.html('<span>▶</span>')
)
.append(
$('<span/>', {
'class': CH_CLASS + ' ' +
CH_PREFIX + 'color-scheme_name'
})
.html(schemes[index].name)
)
.append(
$('<span/>', {
'class': CH_CLASS + ' ' +
CH_PREFIX + 'inline-button ' +
CH_PREFIX + 'color-scheme_add'
})
.html('<span>+</span>')
)
.append(
$('<span/>', {
'class': CH_CLASS + ' ' +
CH_PREFIX + 'inline-button ' +
CH_PREFIX + 'color-scheme_remove'
})
.html('<span>−</span>')
)
)
.append(
$('<div/>', {
'class': CH_CLASS + ' ' +
CH_PREFIX + 'color-scheme_members'
})
.hide()
)
)
// Add member DOM elements to the color scheme.
_AddColorSchemeMembers($newSchemes.eq(index), schemes[index].members);
}
for (var i = 0; i < schemes.length; i++) {
addColorScheme(i);
}
// Add color schemes to DOM.
if (typeof afterTarget === 'undefined') {
COLORHACK.components['color-schemes'].find('#' + CH_PREFIX + 'color-schemes_schemes')
.append($newSchemes);
} else {
$newSchemes.insertAfter(afterTarget);
}
}
// Updates Color Schemes dialog based on internal color schemes in ColorHack object.
var _UpdateColorSchemes = function() {
var $colorSchemes = COLORHACK.components['color-schemes'].find('#' + CH_PREFIX + 'color-schemes_schemes');
$colorSchemes.empty();
_AddColorSchemes(COLORHACK.colorSchemes);
}
var _ToggleColorScheme = function(el) {
var $cs = $(el).closest('.' + CH_PREFIX + 'color-scheme');
var $members = $cs.find('.' + CH_PREFIX + 'color-scheme_members');
if ($cs.hasClass(CH_PREFIX + 'expanded')) {
$cs.removeClass(CH_PREFIX + 'expanded');
$cs.find('.' + CH_PREFIX + 'color-scheme_toggle').css({
'-ms-transform': '',
'-moz-transform': '',
'-webkit-transform': '',
'-o-transform': '',
'transform': ''
})
} else {
$cs.addClass(CH_PREFIX + 'expanded');
$cs.find('.' + CH_PREFIX + 'color-scheme_toggle').css({
'-ms-transform': 'rotate(90deg)',
'-moz-transform': 'rotate(90deg)',
'-webkit-transform': 'rotate(90deg)',
'-o-transform': 'rotate(90deg)',
'transform': 'rotate(90deg)'
})
}
/*if ($members.css('display') === 'none') {
$cs.find('.' + CH_PREFIX + 'color-scheme_toggle span').html('▼');
} else {
$cs.find('.' + CH_PREFIX + 'color-scheme_toggle span').html('▶');
}*/
$members.slideToggle(SLIDE_DURATION, function() {});
}
// Selects color scheme from input color scheme element.
var _SelectColorScheme = function(el) {
var $cs = $(el);
$cs.parent().find('.' + CH_PREFIX + 'color-scheme.' + CH_PREFIX + 'selected').removeClass(CH_PREFIX + 'selected');
$cs.addClass(CH_PREFIX + 'selected');
}
// Removes a color scheme from colorhack and the page by the index.
var _RemoveColorScheme = function(schemeId) {
// Remove from COLORHACK object.
for (var i = 0; i < COLORHACK.colorSchemes.length; i++) {
if (COLORHACK.colorSchemes[i].id === schemeId) {
COLORHACK.colorSchemes.splice(i, 1);
break;
}
}
// Remove from DOM.
COLORHACK.components['color-schemes_schemes'].find('#' + schemeId).remove();
}
// Removes a color scheme member element from colorhack and the page by index.
var _RemoveColorSchemeMember = function(schemeId, memberId) {
// TODO: Add placeholder "No elements assigned to color scheme" member to DOM if none exist (and also remove when adding new members)
// Remove from COLORHACK object.
for (var i = 0; i < COLORHACK.colorSchemes.length; i++) {
if (COLORHACK.colorSchemes[i].id === schemeId) {
for (var j = 0; j < COLORHACK.colorSchemes[i].members.length; j++) {
if (COLORHACK.colorSchemes[i].members[j].id === memberId) {
COLORHACK.colorSchemes[i].members.splice(j, 1);
break;
}
}
break;
}
}
// Remove from DOM
COLORHACK.components['color-schemes_schemes'].find('#' + schemeId).find('#' + memberId).remove();
}
// Fills a given color gradient for a color picker based on input colors.
var _FillGradient = function(colorGradients, color, startHue, stopHue) {
// Get color picker canvas and context.
canvas = colorGradients[color].get(0);
if (typeof canvas === 'undefined') return;
context = canvas.getContext('2d');
if (typeof context === 'undefined') return;
if (color === 'alpha') {
// Fill color picker background with alpha channel grid.
context.fillStyle = '#9F9F9F'; // Light grey
context.fillRect(0, 0, canvas.width, canvas.height);
context.fillStyle = '#636363'; // Dark grey
for (j = 0; j < canvas.height / 8; j++) {
k = (j % 2 === 0) ? 1 : 0;
for (; k < canvas.width / 8; k += 2) {
context.fillRect(k * 8, j * 8, 8, 8);
}
}
}
// Fill color picker with color gradient.
linearGradient = context.createLinearGradient(0, 0, canvas.width, 0);
linearGradient.addColorStop(0, startHue);
linearGradient.addColorStop(1, stopHue);
context.fillStyle = linearGradient;
context.fillRect(0, 0, canvas.width, canvas.height);
}
// Performs all updates needed to change the active color, based on the input rgba object.
var _SetActiveColor = function(rgba) {
var hue = 'rgba(' + rgba.red + ', ' + rgba.green + ', ' + rgba.blue + ', 1)';
var color = 'rgba(' + rgba.red + ', ' + rgba.green + ', ' + rgba.blue + ', ' + rgba.alpha/100 + ')';
http://www.youtube.com/watch?v=KHchLxioU08&feature=player_embedded
// Update hex component colors.
with ({ comps: COLORHACK.components['color-components'] }) {
comps.red.css('background', 'rgba(' + rgba.red + ', 0, 0, 1)');
comps.green.css('background', 'rgba(0, ' + rgba.green + ', 0, 1)');
comps.blue.css('background', 'rgba(0, 0, ' + rgba.blue + ', 1)');
comps.full.css('background', hue);
}
// Update textbox values.
with ({ texts: COLORHACK.components['color-textboxes'] }) {
texts.hex.val(_RgbToHex(rgba));
texts.red.val(rgba.red);
texts.green.val(rgba.green);
texts.blue.val(rgba.blue);
texts.alpha.val(rgba.alpha);
}
// Update alpha color picker gradient color.
_FillGradient(
COLORHACK.components['color-picker_gradients'],
'alpha',
'rgba(' + rgba.red + ', ' + rgba.green + ', ' + rgba.blue + ', 0)',
hue
);
}
// Performs all updates needed when changing the active color in the color settings dialog.
// Uses all color picker selector positions.
var _UpdateActiveColor = function() {
var colors = ['red', 'green', 'blue', 'alpha'];
var rgba = {};
// Get the rgb value for each specific hue
for (var i = 0; i < colors.length; i++) {
// RGB value is based off of selector's position in the color picker.
rgba[colors[i]] = COLORHACK.components['color-pickers'][colors[i]]
.find('.' + CH_PREFIX + 'color-picker_selector').css('left');
rgba[colors[i]] = rgba[colors[i]].substring(0, rgba[colors[i]].length - 2);
if (colors[i] === 'alpha') {
rgba[colors[i]] = Math.floor(rgba[colors[i]] * 100 / 255);
}
}
_SetActiveColor(rgba);
}
// Submits color value entered into color picker textbox.
var _SubmitColorTextbox = function(target, isHex, isAlpha) {
if (isHex) {
// Get the textbox input value as a hex color value.
var val = target.value.substring(0, 6); // value may be too long after copy-paste
if (val.length === 3)
val =
val.substring(0, 1) + val.substring(0, 1) +
val.substring(1, 2) + val.substring(1, 2) +
val.substring(2, 3) + val.substring(2, 3);
else if (val.length !== 6) // note that length <= 6
val = val + Array(6 - val.length + 1).join('0');
var rgb = _HexToRgb(val.toLowerCase());
var $gradients = COLORHACK.components['color-picker_gradients'];
// Set selector positions.
$($gradients.red.data('selector')).css('left', (rgb.r > 255 ? 255 : rgb.r) + 'px');
$($gradients.green.data('selector')).css('left', (rgb.g > 255 ? 255 : rgb.g) + 'px');
$($gradients.blue.data('selector')).css('left', (rgb.b > 255 ? 255 : rgb.b) + 'px');
$($gradients.alpha.data('selector')).css('left', '255px'); // hex colors do not have alpha channel
_UpdateActiveColor();
} else {
// Get the textbox input value as an integer.
var val = parseInt(target.value.replace(/\D/g, '').substring(0, 3), 10); // convert to integer
if (val > 255 && !isAlpha)
val = 255;
else if (val > 100 && isAlpha)
val = 100;
// Set selector position.
$target = $(target);
if (isAlpha)
$($target.data('selector')).css('left', (val / 100 * 255) + 'px');
else
$($target.data('selector')).css('left', val + 'px');
_UpdateActiveColor();
}
}
/*
================================
PUBLIC METHODS
================================
*/
// Builds the DOM for ColorHack elements.
this.BuildDialogs = function() {
$(document.body)
// Add menu to page
.append(this.components.menu)
// Add dialogs to page
.append(this.components['color-schemes'])
.append(this.components['color-settings'])
.append(this.components['colorscheme-details'])
;
}
// Sets up the color picker components.
this.SetupColorPickers = function() {
var colors = [
{ name: 'red', startColor: '#000', stopColor: '#FF0000' },
{ name: 'green', startColor: '#000', stopColor: '#00FF00' },
{ name: 'blue', startColor: '#000', stopColor: '#0000FF' },
{ name: 'alpha', startColor: 'rgba(0, 0, 0, 0)', stopColor: '#000' }
];
// Set up canvas for R, G, and B color pickers.
for (var i = 0; i < colors.length; i++) {
_FillGradient(
this.components['color-picker_gradients'],
colors[i].name,
colors[i].startColor,
colors[i].stopColor
);
}
}
// Creates and attaches events for ColorHack elements.
this.AttachEvents = function() {
/* GLOBAL EVENT VARIABLES */
var drag = {
isDragging: false,
target: null
}
/* GENERAL */
$('body').mouseup(function() {
drag.isDragging = false;
drag.target = null;
})
$('body *').not('.' + CH_CLASS)
// TODO: Make this more robust. Doesn't work as well as element selectors yet in browser development tools yet.
// Highlight elements under mouse when selecting new member to add to color scheme.
.on('mouseover', function(e) { // preferable over mouseenter so that handle is triggered when hover moved to parent element.
if (!_inAddMemberMode)
return;
if (_originalStyling.target) {
// Restore original styling of previously hovered element.
var $prevTarget = _originalStyling.target;
$prevTarget.css('box-shadow', _originalStyling.boxShadow);
}
var $target = $(e.target);
// Save original styling of hovered element.
_originalStyling.target = $target;
_originalStyling.boxShadow = $target.css('box-shadow');
// Set new styling for hovered element.
$target.css('box-shadow', '0 0 2px 2px ' + HIGHLIGHT_BOX_SHADOW_COLOR);
// NOTE: There is an issue here where stylesheet box-shadow styling is overwritten.
// This can be averted by appending the new styling with ", ". However, it would then