LoginSignup
2
1

More than 5 years have passed since last update.

toStringメソッドを使ったカラーコード生成

Posted at
function Color (r, g, b) {
    this.r = r;
    this.g = g;
    this.b = b;
}

Color.prototype.toString = function () {
     return "#"+((((0<<8)+this.r<<8)+this.g<<8)+this.b).toString(16);
};


var red = new Color(255, 0, 0);
console.log(red); //-> #ff0000

var blue = new Color(0, 0, 255);
console.log(blue); //-> #0000ff

var cadetblue = new Color(95, 158, 160);
console.log(cadetblue); //->#5f9ea0
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1