Skip to content

Instantly share code, notes, and snippets.

@riking
Created January 26, 2017 01:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save riking/720b574c9556ff3b6b5a43bf26b9d1f9 to your computer and use it in GitHub Desktop.
Save riking/720b574c9556ff3b6b5a43bf26b9d1f9 to your computer and use it in GitHub Desktop.
color fixing code extracted from ffz
// SOURCE: https://github.com/FrankerFaceZ/FrankerFaceZ/blob/master/src/colors.js
RGBAColor.prototype.luminance = function() {
var r = bit2linear(this.r / 255),
g = bit2linear(this.g / 255),
b = bit2linear(this.b / 255);
return (0.2126 * r) + (0.7152 * g) + (0.0722 * b);
}
HSLAColor.prototype.targetLuminance = function (target) {
var s = this.s;
s *= Math.pow(this.l > 0.5 ? -this.l : this.l - 1, 7) + 1;
var min = 0, max = 1, d = (max - min) / 2, mid = min + d;
for (; d > 1/65536; d /= 2, mid = min + d) {
var luminance = RGBAColor.fromHSLA(this.h, s, mid, 1).luminance()
if (luminance > target) {
max = mid;
} else {
min = mid;
}
}
return new HSLAColor(this.h, s, mid, this.a);
}
// Setup
FFZ.settings_info.luv_contrast = { default: 4.5 /* , ... */ };
this._hslluma_required_bright = this.settings.luv_contrast * (RGBAColor.fromCSS("#17141f").luminance() + 0.05) - 0.05;
this._hslluma_required_dark = (RGBAColor.fromCSS("#efeef1").luminance() + 0.05) / this.settings.luv_contrast - 0.05;
// Color Processing - HSL Luma
if ( this.settings.fix_color === 6 ) {
var lum = rgb.luminance();
if ( lum > this._hslluma_required_dark )
light_color = rgb.toHSLA().targetLuminance(this._hslluma_required_dark).toRGBA();
if ( lum < this._hslluma_required_bright )
dark_color = rgb.toHSLA().targetLuminance(this._hslluma_required_bright).toRGBA();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment