Skip to content

Instantly share code, notes, and snippets.

@azettl
Last active June 18, 2018 09:52
Show Gist options
  • Save azettl/07cdb021fd27a3cb92f59b9a1c215ca8 to your computer and use it in GitHub Desktop.
Save azettl/07cdb021fd27a3cb92f59b9a1c215ca8 to your computer and use it in GitHub Desktop.
String.prototype.splitColor = function(){
var color = this.replace("#", "");
if(color.indexOf(',') == -1 && color.indexOf(' ') == -1){
if(color.length == 6){
return [color.substr(0,2), color.substr(2,2), color.substr(4,2)];
}else if(color.length == 3){
return [color.substr(0,1), color.substr(1,1), color.substr(2,1)];
}
}else{
if(color.indexOf(',') != -1){
color = color.split(',');
return [color[0].trim(), color[1].trim(), color[2].trim()];
}else if(color.indexOf(' ') != -1){
color = color.split(' ');
return [color[0].trim(), color[1].trim(), color[2].trim()];
}
}
}
var color1 = "#FF0000";
var color2 = "F00";
var color3 = "243, 234, 243";
var color4 = "243 234 243";
var color5 = "243,234,243";
console.log(color1.splitColor()); // ["FF", "00", "00"]
console.log(color2.splitColor()); // ["F", "0", "0"]
console.log(color3.splitColor()); // ["243", "234", "243"]
console.log(color4.splitColor()); // ["243", "234", "243"]
console.log(color5.splitColor()); // ["243", "234", "243"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment