Skip to content

Instantly share code, notes, and snippets.

@BlackPrincess
Last active December 11, 2015 20:09
Show Gist options
  • Save BlackPrincess/4653582 to your computer and use it in GitHub Desktop.
Save BlackPrincess/4653582 to your computer and use it in GitHub Desktop.
javascriptでColorクラス。 探しても見つけられなかったので自作してみた
function Color(r, g, b) {
var _r = r ? r : 0;
var _g = g ? g : 0;
var _b = b ? b : 0;
function getColorValue(value){
if(value > 255) {
return 255;
}else if(value < 0) {
return 0;
}else {
return value;
}
}
this.toCssHexCode = function () {
var color = "#" + this.toHexCode();
return color;
};
this.toHexCode = function () {
return Color.getHexCode(_r,_g,_b);
};
this.toRGB = function () {
return { r: _r,
g: _g,
b: _b
};
};
//mutable
this.additiveBlend = function(color){
var _rgb = color.toRGB();
_r += _rgb.r;
_g += _rgb.g;
_b += _rgb.b;
_r = getColorValue(_r);
_g = getColorValue(_g);
_b = getColorValue(_b);
};
//immutable
this.additiveBlendColor = function(color){
var _rgb = color.toRGB();
var r = getColorValue(_r + _rgb.r);
var g = getColorValue(_g + _rgb.g);
var b = getColorValue(_b + _rgb.b);
return new Color(r, g, b);
};
}
Color.getRGB = function(hexCode){
//#00ffffと00ffff
var _hexCode = hexCode.substr(hexCode.length - 6, 6);
var _rHex = _hexCode.substring(0, 2);
var _gHex = _hexCode.substring(2, 4);
var _bHex = _hexCode.substring(4, 6);
var _r = parseInt(_rHex, 16);
var _g = parseInt(_gHex, 16);
var _b = parseInt(_bHex, 16);
return {
r:_r,
g:_g,
b:_b
};
};
Color.getHexCode = function(r, g, b){
function getHexPaddingString(v) {
var temp = "00" + parseInt(v).toString(16);
temp = temp.substr(temp.length - 2, 2);
return temp;
}
var _rHexCode = getHexPaddingString(r);
var _gHexCode = getHexPaddingString(g);
var _bHexCode = getHexPaddingString(b);
var _hexCode = "" + _rHexCode + _gHexCode + _bHexCode;
return _hexCode;
};
Color.getCssHexCode = function(r, g, b){
return "#" + Color.getHexCode(r, g, b);
};
Color.create = function (r, g, b) {
return new Color(r, g, b);
};
Color.createFromRGB = function (rgb) {
return new Color(rgb.r, rgb.g, rgb.b);
};
Color.createFromHexCode = function (hexCode) {
var rgb = Color.getRGB(hexCode);
return new Color(rgb.r, rgb.g, rgb.b);
};
TestCase("Test",{
"test Color.toHexCode. black": function(){
var result = new Color(0, 0, 0).toHexCode();
assertString(result);
assertEquals("000000", result);
},
"test Color.toHexCode. mixed color": function(){
var result = new Color(0, 255, 15).toHexCode();
assertString(result);
assertEquals("00ff0f", result);
},
"test Color.toHexCode. white": function(){
var result = new Color(255, 255, 255).toHexCode();
assertString(result);
assertEquals("ffffff", result);
},
"test Color.toCssHexCode": function(){
var result = new Color(0, 0, 0).toCssHexCode();
assertString(result);
assertEquals("#000000", result);
},
"test Color.additiveBlend" : function(){
var target = new Color(100,0,0);
var addition = new Color(100,100,100);
target.additiveBlend(addition);
assertEquals({r:200,g:100,b:100},target.toRGB());
},
"test Color.additiveBlendColor" : function(){
var target = new Color(100,0,0);
var addition = new Color(100,100,100);
var actual = target.additiveBlendColor(addition);
assertEquals({r:200,g:100,b:100},actual.toRGB());
},
/* static */
"test Color.toRGB. black": function(){
var result = new Color(0, 0, 0).toRGB();
assertEquals({r:0, g:0, b:0}, result);
},
"test Color.toRGB. mixed color": function(){
var result = new Color(0, 255, 15).toRGB();
assertEquals({r:0, g:255, b:15}, result);
},
"test Color.create": function(){
var result = Color.create(0,255,15).toHexCode()
assertEquals("00ff0f", result);
},
"test Color.createFromRGB": function(){
var result = Color.createFromRGB({r:0,g:255,b:15}).toHexCode()
assertEquals("00ff0f", result);
},
"test Color.createFromHexCode": function(){
var result = Color.createFromHexCode("0f0f0f").toHexCode()
assertEquals("0f0f0f", result);
},
"test Color.createFromHexCode. with sharp": function(){
var result = Color.createFromHexCode("#0f0f0f").toHexCode()
assertEquals("0f0f0f", result);
},
"test Color.getRGB " : function(){
var result = Color.getRGB("00ff0f");
assertEquals({r:0,g:255,b:15},result);
},
"test Color.getHexCode " : function(){
var result = Color.getHexCode(0,255,15);
assertString(result);
assertEquals("00ff0f",result);
},
"test Color.getCssHexCode " : function(){
var result = Color.getCssHexCode(0,255,15);
assertString(result);
assertEquals("#00ff0f",result);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment