From ef19f4e562cfca7d5eca50e22283d94ff890a1d7 Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Sat, 15 Sep 2018 15:08:49 +1200 Subject: Minor cleanup --- benchmark/scripts/color.js | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 benchmark/scripts/color.js (limited to 'benchmark/scripts/color.js') diff --git a/benchmark/scripts/color.js b/benchmark/scripts/color.js new file mode 100644 index 0000000..074c963 --- /dev/null +++ b/benchmark/scripts/color.js @@ -0,0 +1,39 @@ +function Color(hex = '#000000') +{ + this.hex = hex; + + var r = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(this.hex); + + this.rgb = {r: parseInt(r[1], 16),g: parseInt(r[2], 16),b: parseInt(r[3], 16)} + + this.r = this.rgb.r; + this.g = this.rgb.g; + this.b = this.rgb.b; + + this.average = parseInt((this.rgb.r + this.rgb.g + this.rgb.b)/3) + this.invert = {r:255-this.rgb.r, g:255-this.rgb.g, b:255-this.rgb.b}; + + this.contrast = function(b) + { + let a_lum = 0.2126 * _linear(this.r / 256) + 0.7152 * _linear(this.g / 256) + 0.0722 * _linear(this.b / 256) + let b_lum = 0.2126 * _linear(b.r / 256) + 0.7152 * _linear(b.g / 256) + 0.0722 * _linear(b.b / 256) + return a_lum > b_lum ? (a_lum + 0.05) / (b_lum + 0.05) : (b_lum + 0.05) / (a_lum + 0.05); + } + + this.rgba = function() + { + return "rgba("+this.rgb().r+","+this.rgb().g+","+this.rgb().b+",1)"; + } + + this.floats = function() + { + return { r:this.rgb.r/255, g:this.rgb.g/255, b:this.rgb.b/255 } + } + + this.toString = function() + { + return this.hex; + } + + function _linear(v){ return (v <= 0.03928) ? v / 12.92 : Math.pow((v + 0.055) / 1.055, 2.4); } +} \ No newline at end of file -- cgit v1.2.3