etherpad-lite/src/static/js/colorutils.js

122 lines
4 KiB
JavaScript
Raw Normal View History

2020-12-22 16:07:17 +01:00
'use strict';
/**
* This code is mostly from the old Etherpad. Please help us to comment this code.
* This helps other people to understand this code better and helps them to improve it.
* TL;DR COMMENTS ON THIS FILE ARE HIGHLY APPRECIATED
*/
2011-03-26 14:10:41 +01:00
// DO NOT EDIT THIS FILE, edit infrastructure/ace/www/colorutils.js
// THIS FILE IS ALSO SERVED AS CLIENT-SIDE JS
/**
* Copyright 2009 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2020-11-23 19:24:19 +01:00
const colorutils = {};
2011-03-26 14:10:41 +01:00
// Check that a given value is a css hex color value, e.g.
// "#ffffff" or "#fff"
2020-12-22 16:07:17 +01:00
colorutils.isCssHex = (cssColor) => /^#([0-9a-f]{3}|[0-9a-f]{6})$/i.test(cssColor);
2011-03-26 14:10:41 +01:00
// "#ffffff" or "#fff" or "ffffff" or "fff" to [1.0, 1.0, 1.0]
2020-12-22 16:07:17 +01:00
colorutils.css2triple = (cssColor) => {
2020-11-23 19:24:19 +01:00
const sixHex = colorutils.css2sixhex(cssColor);
2011-07-07 19:59:34 +02:00
2020-12-22 16:07:17 +01:00
const hexToFloat = (hh) => Number(`0x${hh}`) / 255;
return [
hexToFloat(sixHex.substr(0, 2)),
hexToFloat(sixHex.substr(2, 2)),
hexToFloat(sixHex.substr(4, 2)),
];
2020-11-23 19:24:19 +01:00
};
2011-03-26 14:10:41 +01:00
// "#ffffff" or "#fff" or "ffffff" or "fff" to "ffffff"
2020-12-22 16:07:17 +01:00
colorutils.css2sixhex = (cssColor) => {
2020-11-23 19:24:19 +01:00
let h = /[0-9a-fA-F]+/.exec(cssColor)[0];
2020-12-22 16:07:17 +01:00
if (h.length !== 6) {
2020-11-23 19:24:19 +01:00
const a = h.charAt(0);
const b = h.charAt(1);
const c = h.charAt(2);
2011-07-07 19:59:34 +02:00
h = a + a + b + b + c + c;
2011-03-26 14:10:41 +01:00
}
return h;
2020-11-23 19:24:19 +01:00
};
2011-03-26 14:10:41 +01:00
// [1.0, 1.0, 1.0] -> "#ffffff"
2020-12-22 16:07:17 +01:00
colorutils.triple2css = (triple) => {
const floatToHex = (n) => {
2020-11-23 19:24:19 +01:00
const n2 = colorutils.clamp(Math.round(n * 255), 0, 255);
return (`0${n2.toString(16)}`).slice(-2);
2020-12-22 16:07:17 +01:00
};
2020-11-23 19:24:19 +01:00
return `#${floatToHex(triple[0])}${floatToHex(triple[1])}${floatToHex(triple[2])}`;
};
2011-03-26 14:10:41 +01:00
2020-12-22 16:07:17 +01:00
colorutils.clamp = (v, bot, top) => v < bot ? bot : (v > top ? top : v);
colorutils.min3 = (a, b, c) => (a < b) ? (a < c ? a : c) : (b < c ? b : c);
colorutils.max3 = (a, b, c) => (a > b) ? (a > c ? a : c) : (b > c ? b : c);
colorutils.colorMin = (c) => colorutils.min3(c[0], c[1], c[2]);
colorutils.colorMax = (c) => colorutils.max3(c[0], c[1], c[2]);
colorutils.scale = (v, bot, top) => colorutils.clamp(bot + v * (top - bot), 0, 1);
colorutils.unscale = (v, bot, top) => colorutils.clamp((v - bot) / (top - bot), 0, 1);
2011-03-26 14:10:41 +01:00
2020-12-22 16:07:17 +01:00
colorutils.scaleColor = (c, bot, top) => [
colorutils.scale(c[0], bot, top),
colorutils.scale(c[1], bot, top),
colorutils.scale(c[2], bot, top),
];
2011-03-26 14:10:41 +01:00
2020-12-22 16:07:17 +01:00
colorutils.unscaleColor = (c, bot, top) => [
colorutils.unscale(c[0], bot, top),
colorutils.unscale(c[1], bot, top),
colorutils.unscale(c[2], bot, top),
];
2011-03-26 14:10:41 +01:00
2020-12-22 16:07:17 +01:00
// rule of thumb for RGB brightness; 1.0 is white
colorutils.luminosity = (c) => c[0] * 0.30 + c[1] * 0.59 + c[2] * 0.11;
2011-03-26 14:10:41 +01:00
2020-12-22 16:07:17 +01:00
colorutils.saturate = (c) => {
2020-11-23 19:24:19 +01:00
const min = colorutils.colorMin(c);
const max = colorutils.colorMax(c);
2011-03-26 14:10:41 +01:00
if (max - min <= 0) return [1.0, 1.0, 1.0];
return colorutils.unscaleColor(c, min, max);
2020-11-23 19:24:19 +01:00
};
2011-03-26 14:10:41 +01:00
2020-12-22 16:07:17 +01:00
colorutils.blend = (c1, c2, t) => [
colorutils.scale(t, c1[0], c2[0]),
colorutils.scale(t, c1[1], c2[1]),
colorutils.scale(t, c1[2], c2[2]),
];
2020-12-22 16:07:17 +01:00
colorutils.invert = (c) => [1 - c[0], 1 - c[1], 1 - c[2]];
2020-12-22 16:07:17 +01:00
colorutils.complementary = (c) => {
2020-11-23 19:24:19 +01:00
const inv = colorutils.invert(c);
return [
(inv[0] >= c[0]) ? Math.min(inv[0] * 1.30, 1) : (c[0] * 0.30),
(inv[1] >= c[1]) ? Math.min(inv[1] * 1.59, 1) : (c[1] * 0.59),
2020-11-23 19:24:19 +01:00
(inv[2] >= c[2]) ? Math.min(inv[2] * 1.11, 1) : (c[2] * 0.11),
];
2020-11-23 19:24:19 +01:00
};
2020-12-22 16:07:17 +01:00
colorutils.textColorFromBackgroundColor = (bgcolor, skinName) => {
const white = skinName === 'colibris' ? 'var(--super-light-color)' : '#fff';
const black = skinName === 'colibris' ? 'var(--super-dark-color)' : '#222';
return colorutils.luminosity(colorutils.css2triple(bgcolor)) < 0.5 ? white : black;
2020-11-23 19:24:19 +01:00
};
exports.colorutils = colorutils;