Skip to content

Commit a7ae3e5

Browse files
Bug fixes and storing palettes in local storage
-Fixed bug where custom colour would not be deleted if colour was rgba with an opacity of 1. -Storing custom palettes between refreshes and page changes.
1 parent 1dd5387 commit a7ae3e5

18 files changed

+353
-77
lines changed

ColorPicker.bsdesign

494 Bytes
Binary file not shown.

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,16 @@ Uses Jquery and Bootstrap.
1313
+ Colour wall
1414
+ Palettes
1515
+ Built in palettes
16-
+ Custom Palettes
16+
+ Custom Palettes
1717
+ Importing/Exporting
1818
+ All used Colors stored in the "Current" palette
1919
+ "Session" stores favourites for the session only.
20-
+ Linking with Database (Not Implemented)
20+
+ Linking with Database ***1**
2121
+ RGB or HEX Html
2222
+ RGB or HSL Controls
2323
+ Themes (Sort of, with CSS... Only partially implemented)
2424

25+
***1** `Not implemented at this time`
2526

2627
## Required References
2728

assets/js/ColorPicker/ColorPicker.js

Lines changed: 126 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -712,26 +712,41 @@ transparency">Transparency</label>\
712712
});
713713

714714
//Delete the palette when the delete button is clicked
715-
ColorPicker.Object.favDelete.on('click', function(){
716-
717-
//First delete the palette while we still have the selected option
718-
ColorPicker.Object.palettes.find('[palette='+ColorPicker.Object.favouriteSelect.val()+']').remove();
719-
720-
//Delete the array
721-
delete ColorPicker.Object.userColors[ColorPicker.Object.favouriteSelect.val()];
722-
723-
//After we remove the palette we can remove the option from the select
724-
ColorPicker.Object.favouriteSelect.find(':selected').remove();
715+
ColorPicker.Object.favDelete.on('click', function(){
716+
ColorPicker.DeletePalette(ColorPicker.Object.favouriteSelect.val());
717+
});
718+
719+
}
720+
721+
static DeletePalette(name){
722+
723+
if(ColorPicker.Object.userColors[name] == null) return;
724+
725+
//First delete the palette while we still have the selected option
726+
ColorPicker.Object.palettes.find('[palette='+name+']').remove();
725727

726-
//Set the favourite select back to the old palette
727-
ColorPicker.Object.favouriteSelect.val(ColorPicker.Object.favouriteSelect.find('option').val());
728+
//Delete the array
729+
delete ColorPicker.Object.userColors[name];
728730

729-
//Hide the button
730-
ColorPicker.Object.favDelete.removeClass('show');
731+
//After we remove the palette we can remove the option from the select
732+
ColorPicker.Object.favouriteSelect.find(':selected').remove();
731733

734+
//Set the favourite select back to the old palette
735+
ColorPicker.Object.favouriteSelect.val(ColorPicker.Object.favouriteSelect.find('option').val());
732736

733-
});
737+
//Hide the button
738+
ColorPicker.Object.favDelete.removeClass('show');
739+
}
740+
741+
static ClearPalette(name){
742+
if(ColorPicker.Object.userColors[name] == null) return;
743+
744+
//First delete the palette while we still have the selected option
745+
ColorPicker.Object.palettes.find('[palette='+name+']').html('');
734746

747+
//Delete the array and recreate it
748+
delete ColorPicker.Object.userColors[name];
749+
ColorPicker.Object.userColors[name] = [];
735750
}
736751

737752
//Setup events for importing and exporting palettes.
@@ -759,6 +774,73 @@ transparency">Transparency</label>\
759774

760775
}
761776

777+
static AddPallete(name, colors){
778+
779+
if(colors instanceof String)
780+
colors = JSON.parse(colors);
781+
782+
if(Object.keys(colors)[0] == name)
783+
colors = colors[name];
784+
785+
//attribute checker : just checking to see if the palette exists
786+
var ac = $('[palette='+ name +']').attr('palette');
787+
788+
//If there is no palette with the name then we need to create one
789+
if(ac == null || ac == 'undefined'){
790+
ColorPicker.CreatePalette(name);
791+
}else{
792+
ColorPicker.ActivateFavourite(name);
793+
ColorPicker.ClearPalette(name);
794+
}
795+
796+
//Overwrite the palette with our new array
797+
ColorPicker.Object.userColors[name] = colors;
798+
799+
//Log it to the console
800+
console.log('Loaded Palette : ' + name + " | Colors : " + colors.length);
801+
802+
//Create all the color elements so the user can select them.
803+
for(var i = 0; i < colors.length; i++){
804+
ColorPicker.CreateColorElement($('[palette='+ name +']'), colors[i]);
805+
}
806+
807+
ColorPicker.StorePalette(name, colors);
808+
}
809+
810+
//Store use palette in local storage
811+
static StorePalette(name, colors){
812+
var palObj = localStorage.getItem('palettes');
813+
if(palObj == null)
814+
palObj = {};
815+
else
816+
palObj = JSON.parse(palObj);
817+
818+
palObj[name] = colors;
819+
820+
var jpalettes = JSON.stringify(palObj, null, 2);
821+
822+
localStorage.setItem('palettes', jpalettes);
823+
}
824+
825+
//Store all user palettes in local storage
826+
static StorePalettes(){
827+
for(let key in ColorPicker.Object.userColors){
828+
StorePalette(name, ColorPicker.Object.userColors[key]);
829+
}
830+
}
831+
832+
static LoadLocalPalettes(){
833+
var palettes = localStorage.getItem('palettes');
834+
835+
if(palettes == null) return;
836+
837+
var palObj = JSON.parse(palettes);
838+
839+
for(let key in palObj){
840+
ColorPicker.AddPallete(key, palObj[key]);
841+
}
842+
}
843+
762844
//Imports a .pal file and creates the palette.
763845
static ImportFavouritePal(){
764846

@@ -789,28 +871,11 @@ transparency">Transparency</label>\
789871
//The reason we aren't using JSON.parse is because we want the user to be able to edit
790872
//and add colors easily, and directly to the .pal file.
791873
//All they need to do is add a color in web format (rgb, rgba, hex) and seperate them with |
792-
var colors = fr.result.split('|');//JSON.parse(fr.result);
874+
var colors = JSON.parse(fr.result);
793875

794-
//Overwrite the palette with our new array
795-
ColorPicker.Object.userColors[palette] = colors;
876+
var name = Object.keys(colors)[0];
796877

797-
//attribute checker : just checking to see if the palette exists
798-
var ac = $('[palette='+ palette +']').attr('palette');
799-
800-
//If there is no palette with the name then we need to create one
801-
if(ac == null || ac == 'undefined'){
802-
ColorPicker.CreatePalette(palette);
803-
}else{
804-
ColorPicker.ActivateFavourite(palette);
805-
}
806-
807-
//Log it to the console
808-
console.log('Loaded Palette : ' + palette + " | Colors : " + colors.length);
809-
810-
//Create all the color elements so the user can select them.
811-
for(var i = 0; i < colors.length; i++){
812-
ColorPicker.CreateColorElement($('[palette='+ palette +']'), colors[i]);
813-
}
878+
ColorPicker.AddPallete(name, colors);
814879

815880
};
816881

@@ -822,34 +887,39 @@ transparency">Transparency</label>\
822887
//Exports the current favourite palette as a .pal file.
823888
static ExportFavouritePal() {
824889

890+
//Get name of palette and store
891+
let name = ColorPicker.Object.favouriteSelect.val();
892+
893+
//Store palette in temporary object.
894+
let tempObj = {};
895+
tempObj[name] = ColorPicker.Object.userColors[name];
896+
825897
//Create the variable which we will store the array in (as a string)
826-
//The reason we aren't using JSON.stringify is because we want the user to be able to edit
827-
//and add colors easily, and directly to the .pal file.
828-
//All they need to do is add a color in web format (rgb, rgba, hex) and seperate them with |
829-
let dataStr = ""; //JSON.stringify(ColorPicker.Object.userColors[ColorPicker.Object.favouriteSelect.val()]);
898+
let dataStr = JSON.stringify(tempObj, null, 2);
899+
830900

831-
//First we need to convert the color array into a JSON string.
901+
`//First we need to convert the color array into a JSON string.
832902
$.each(ColorPicker.Object.userColors[ColorPicker.Object.favouriteSelect.val()], function(index, value){
833903
dataStr += value;
834904
835905
//We don't want to add the splitter if its the last one, otherwise on import we will get a blank color
836906
if(ColorPicker.Object.userColors[ColorPicker.Object.favouriteSelect.val()].length - 1 != index)
837907
dataStr + '|';
838-
});
908+
}); `
839909

840-
//Then we will encode the json string into a temporary file.
910+
//Encode the json string into a temporary file.
841911
let dataUri = 'data:application/json;charset=utf-8,'+ encodeURIComponent(dataStr);
842912

843-
//Now we need to set our hidden links, link to our temporary file
913+
//Set our Gidden links, and link to our temporary file
844914
ColorPicker.Object.favExportInput.setAttribute('href', dataUri);
845915

846916
//Create a download attribute that has the name of our file
847-
ColorPicker.Object.favExportInput.setAttribute('download', ColorPicker.Object.favouriteSelect.val() + '.pal');
917+
ColorPicker.Object.favExportInput.setAttribute('download', name + '.pal');
848918

849919
//Simulate a click on the link to begin the download
850920
ColorPicker.Object.favExportInput.click();
851921

852-
console.log('Exported Palette : ' + ColorPicker.Object.favouriteSelect.text() + " | Colors : " + ColorPicker.Object.userColors[ColorPicker.Object.favouriteSelect.val()].length);
922+
console.log('Exported Palette : ' + name + " | Colors : " + tempObj[name].length);
853923
}
854924

855925
//Add or remove current color from the selected palette.
@@ -964,7 +1034,9 @@ transparency">Transparency</label>\
9641034

9651035
//Create the element and append it to the palette
9661036
ColorPicker.CreateColorElement($('[palette='+ list +']'), color);
967-
}
1037+
}
1038+
1039+
ColorPicker.StorePalette(list, ColorPicker.Object.userColors[list]);
9681040
}
9691041

9701042
}
@@ -988,8 +1060,10 @@ transparency">Transparency</label>\
9881060
ColorPicker.Object.userColors[list].splice(index, 1);
9891061
//ColorPicker.Object.userColors[list].splice(ColorPicker.Object.userColors[list].indexOf(color), 1);
9901062
//$('[palette='+list+']').find('[style*="background-color: '+ color +';"]').remove();
991-
$('[value="'+color+'"]').remove();
1063+
//$('#cp_palettes [value="'+color+'"]').remove();
1064+
$('#cp_palettes [palette="'+list+'"]').children().eq(index).remove();
9921065
//$('[palette='+list+']').find('[style*="box-shadow: '+ color +' '+ ColorPicker.Object.boxshadow_overlay +';"], [value="'+color+'"]').remove();
1066+
ColorPicker.StorePalette(list, ColorPicker.Object.userColors[list]);
9931067
}
9941068

9951069
/*
@@ -2174,6 +2248,11 @@ transparency">Transparency</label>\
21742248
//ColorPicker.CreateColorElement($('[palette=simple]'), Colors["simple"][i]);
21752249
}
21762250

2251+
//Load user palettes
2252+
ColorPicker.LoadLocalPalettes();
2253+
2254+
ColorPicker.ActivateFavourite('favourite');
2255+
21772256
}
21782257

21792258

assets/js/Example.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,17 @@ $(document).ready(function(){
1919
$('#example_heading').css('color', color);
2020
});
2121

22+
//Will change value after done moving.
23+
$('#testSlider').on('change', function(event, value){
24+
$('#example_heading2').html('Change my value : ' + value);
25+
});
26+
27+
//Will constantly change value/
28+
$('#testSlider2').on('changing', function(event, value){
29+
$('#example_heading2').html('Change my value : ' + value);
30+
});
31+
32+
33+
34+
2235
});

assets/js/delegates/CustomEvents.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
var events = {};
2+
3+
function invokeEvent(name){
4+
events[name].Invoke();
5+
}
6+
7+
function onEvent(name, fn){
8+
events[name].add(fn);
9+
}
10+
11+
function removeFromEvent(name, fn){
12+
events[name].delete(fn);
13+
}
14+
15+
function createEvent(name){
16+
events[name] = new Set();
17+
}
18+
19+
createEvent('auth');
20+
createEvent('reset');
21+
createEvent('resetting');
22+
createEvent('status');
23+
24+
25+
26+

assets/js/delegates/EventKeyDown.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var EventKeyDown = new Set();
22

3-
//Create the event to call our functions.
3+
//Create the event to call the functions in the set.
44
$(document).keydown(CallEventKeyDown);
55

66
function CallEventKeyDown(e){

assets/js/delegates/EventKeyPress.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var EventKeyPress = new Set();
22

3-
//Create the event to call our functions.
3+
//Create the event to call the functions in the set.
44
$(document).keypress(CallEventKeyPress);
55

66
function CallEventKeyPress(e){

assets/js/delegates/EventKeyUp.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var EventKeyUp = new Set();
22

3-
//Create the event to call our functions.
3+
//Create the event to call the functions in the set.
44
$(document).keyup(CallEventKeyUp);
55

66
function CallEventKeyUp(e){

assets/js/delegates/EventMouseClick.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var EventMouseClick = new Set();
22

3-
//Create the event to call our functions.
3+
//Create the event to call the functions in the set.
44
$(document).on('click',CallEventMouseClick);
55

66
function CallEventMouseClick(e){

assets/js/delegates/EventMouseDoubleClick.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var EventMouseDoubleClick = new Set();
22

3-
//Create the event to call our functions.
3+
//Create the event to call the functions in the set.
44
$(document).on('dblclick',CallEventMouseDoubleClick);
55

66
function CallEventMouseDoubleClick(e){

assets/js/delegates/EventMouseDown.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var EventMouseDown = new Set();
22

33
var isMouseDown = false;
44

5-
//Create the event to call our functions.
5+
//Create the event to call the functions in the set.
66
$(document).mousedown(CallEventMouseDown);
77

88
function CallEventMouseDown(e){

assets/js/delegates/EventMouseMove.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var EventMouseMove = new Set();
22

3-
//Create the event to call our functions.
3+
//Create the event to call the functions in the set.
44
$(document).mousemove(CallEventMouseMove);
55

66
function CallEventMouseMove(e){

assets/js/delegates/EventMouseUp.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
//EventMouseUp.js must be included after EventMouseDown
12
var EventMouseUp = new Set();
23

3-
//Create the event to call our functions.
4+
//Create the event to call the functions in the set.
45
$(document).mouseup(CallEventMouseUp);
56

67
function CallEventMouseUp(e){

assets/js/delegates/EventReady.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var EventReady = new Set();
22

3-
//Create the event to call our functions.
3+
//Create the event to call the functions in the set.
44
$(document).ready(CallEventReady);
55

66
function CallEventReady(e){

assets/js/delegates/EventWindowResize.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var EventWindowResize = new Set();
22

3-
//Create the event to call our functions.
3+
//Create the event to call the functions in the set.
44
$(window).resize(CallEventWindowResize);
55

66
function CallEventWindowResize(e){

0 commit comments

Comments
 (0)