diff options
| author | neauoire <aliceffekt@gmail.com> | 2019-11-09 10:47:37 -0500 |
|---|---|---|
| committer | neauoire <aliceffekt@gmail.com> | 2019-11-09 10:47:37 -0500 |
| commit | a0015b535b78ae49e39af1c76bf53b70c14d20b4 (patch) | |
| tree | d8b282ddcb7d2d94c374e4e41de4605197833b52 /editor/scripts/color.js | |
| parent | 3226de0b193d505ebe0504f263f59cd64e24597c (diff) | |
*
Diffstat (limited to 'editor/scripts/color.js')
| -rw-r--r-- | editor/scripts/color.js | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/editor/scripts/color.js b/editor/scripts/color.js new file mode 100644 index 0000000..8296e1e --- /dev/null +++ b/editor/scripts/color.js @@ -0,0 +1,34 @@ +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) { + const a_lum = 0.2126 * _linear(this.r / 256) + 0.7152 * _linear(this.g / 256) + 0.0722 * _linear(this.b / 256) + const 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) } +} |
