From 1c7c1ad6a734a8a79947b6ed40b0de3031571fce Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Wed, 24 Feb 2021 14:55:55 -0800 Subject: [PATCH] stream_color: Move get_color_class definition before use. Signed-off-by: Anders Kaseorg --- static/js/stream_color.js | 106 +++++++++++++++++++------------------- 1 file changed, 53 insertions(+), 53 deletions(-) diff --git a/static/js/stream_color.js b/static/js/stream_color.js index 27f59242bc..309fea874c 100644 --- a/static/js/stream_color.js +++ b/static/js/stream_color.js @@ -9,6 +9,59 @@ exports.default_color = "#c2c2c2"; // Classes which could be returned by get_color_class. exports.color_classes = "dark_background"; +let lightness_threshold; +exports.initialize = function () { + // sRGB color component for dark label text. + // 0x33 to match the color #333333 set by Bootstrap. + const label_color = 0x33; + const lightness = colorspace.luminance_to_lightness(colorspace.sRGB_to_linear(label_color)); + + // Compute midpoint lightness between that and white (100). + lightness_threshold = (lightness + 100) / 2; +}; + +// From a background color (in format "#fff" or "#ffffff") +// pick a CSS class (or empty string) to determine the +// text label color etc. +// +// It would be better to work with an actual data structure +// rather than a hex string, but we have to deal with values +// already saved on the server, etc. +// +// This gets called on every message, so cache the results. +exports.get_color_class = _.memoize((color) => { + let match; + let i; + const channel = [0, 0, 0]; + let mult = 1; + + match = /^#([\dA-Fa-f]{2})([\dA-Fa-f]{2})([\dA-Fa-f]{2})$/.exec(color); + if (!match) { + // 3-digit shorthand; Spectrum gives this e.g. for pure black. + // Multiply each digit by 16+1. + mult = 17; + + match = /^#([\dA-Fa-f])([\dA-Fa-f])([\dA-Fa-f])$/.exec(color); + if (!match) { + // Can't understand color. + return ""; + } + } + + // CSS colors are specified in the sRGB color space. + // Convert to linear intensity values. + for (i = 0; i < 3; i += 1) { + channel[i] = colorspace.sRGB_to_linear(mult * Number.parseInt(match[i + 1], 16)); + } + + // Compute perceived lightness as CIE L*. + const lightness = colorspace.luminance_to_lightness(colorspace.rgb_luminance(channel)); + + // Determine if we're past the midpoint between the + // dark and light label lightness. + return lightness < lightness_threshold ? "dark_background" : ""; +}); + function update_table_stream_color(table, stream_name, color) { // This is ugly, but temporary, as the new design will make it // so that we only have color in the headers. @@ -133,57 +186,4 @@ exports.sidebar_popover_colorpicker_options_full = { change: picker_do_change_color, }; -let lightness_threshold; -exports.initialize = function () { - // sRGB color component for dark label text. - // 0x33 to match the color #333333 set by Bootstrap. - const label_color = 0x33; - const lightness = colorspace.luminance_to_lightness(colorspace.sRGB_to_linear(label_color)); - - // Compute midpoint lightness between that and white (100). - lightness_threshold = (lightness + 100) / 2; -}; - -// From a background color (in format "#fff" or "#ffffff") -// pick a CSS class (or empty string) to determine the -// text label color etc. -// -// It would be better to work with an actual data structure -// rather than a hex string, but we have to deal with values -// already saved on the server, etc. -// -// This gets called on every message, so cache the results. -exports.get_color_class = _.memoize((color) => { - let match; - let i; - const channel = [0, 0, 0]; - let mult = 1; - - match = /^#([\dA-Fa-f]{2})([\dA-Fa-f]{2})([\dA-Fa-f]{2})$/.exec(color); - if (!match) { - // 3-digit shorthand; Spectrum gives this e.g. for pure black. - // Multiply each digit by 16+1. - mult = 17; - - match = /^#([\dA-Fa-f])([\dA-Fa-f])([\dA-Fa-f])$/.exec(color); - if (!match) { - // Can't understand color. - return ""; - } - } - - // CSS colors are specified in the sRGB color space. - // Convert to linear intensity values. - for (i = 0; i < 3; i += 1) { - channel[i] = colorspace.sRGB_to_linear(mult * Number.parseInt(match[i + 1], 16)); - } - - // Compute perceived lightness as CIE L*. - const lightness = colorspace.luminance_to_lightness(colorspace.rgb_luminance(channel)); - - // Determine if we're past the midpoint between the - // dark and light label lightness. - return lightness < lightness_threshold ? "dark_background" : ""; -}); - window.stream_color = exports;