|
| 1 | +/* |
| 2 | +Copyright 2019 Michael Telatynski <[email protected]> |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +export function textToHtmlRainbow(str: string): string { |
| 18 | + const frequency = (2 * Math.PI) / str.length; |
| 19 | + |
| 20 | + return Array.from(str) |
| 21 | + .map((c, i) => { |
| 22 | + if (c === " ") { |
| 23 | + return c; |
| 24 | + } |
| 25 | + const [a, b] = generateAB(i * frequency, 1); |
| 26 | + const [red, green, blue] = labToRGB(75, a, b); |
| 27 | + return ( |
| 28 | + '<font color="#' + |
| 29 | + red.toString(16).padStart(2, "0") + |
| 30 | + green.toString(16).padStart(2, "0") + |
| 31 | + blue.toString(16).padStart(2, "0") + |
| 32 | + '">' + |
| 33 | + c + |
| 34 | + "</font>" |
| 35 | + ); |
| 36 | + }) |
| 37 | + .join(""); |
| 38 | +} |
| 39 | + |
| 40 | +function generateAB(hue: number, chroma: number): [number, number] { |
| 41 | + const a = chroma * 127 * Math.cos(hue); |
| 42 | + const b = chroma * 127 * Math.sin(hue); |
| 43 | + |
| 44 | + return [a, b]; |
| 45 | +} |
| 46 | + |
| 47 | +function labToRGB(l: number, a: number, b: number): [number, number, number] { |
| 48 | + // https://en.wikipedia.org/wiki/CIELAB_color_space#Reverse_transformation |
| 49 | + // https://en.wikipedia.org/wiki/SRGB#The_forward_transformation_(CIE_XYZ_to_sRGB) |
| 50 | + |
| 51 | + // Convert CIELAB to CIEXYZ (D65) |
| 52 | + let y = (l + 16) / 116; |
| 53 | + const x = adjustXYZ(y + a / 500) * 0.9505; |
| 54 | + const z = adjustXYZ(y - b / 200) * 1.089; |
| 55 | + |
| 56 | + y = adjustXYZ(y); |
| 57 | + |
| 58 | + // Linear transformation from CIEXYZ to RGB |
| 59 | + const red = 3.24096994 * x - 1.53738318 * y - 0.49861076 * z; |
| 60 | + const green = -0.96924364 * x + 1.8759675 * y + 0.04155506 * z; |
| 61 | + const blue = 0.05563008 * x - 0.20397696 * y + 1.05697151 * z; |
| 62 | + |
| 63 | + return [adjustRGB(red), adjustRGB(green), adjustRGB(blue)]; |
| 64 | +} |
| 65 | + |
| 66 | +function adjustXYZ(v: number): number { |
| 67 | + if (v > 0.2069) { |
| 68 | + return Math.pow(v, 3); |
| 69 | + } |
| 70 | + return 0.1284 * v - 0.01771; |
| 71 | +} |
| 72 | + |
| 73 | +function gammaCorrection(v: number): number { |
| 74 | + // Non-linear transformation to sRGB |
| 75 | + if (v <= 0.0031308) { |
| 76 | + return 12.92 * v; |
| 77 | + } |
| 78 | + return 1.055 * Math.pow(v, 1 / 2.4) - 0.055; |
| 79 | +} |
| 80 | + |
| 81 | +function adjustRGB(v: number): number { |
| 82 | + const corrected = gammaCorrection(v); |
| 83 | + |
| 84 | + // Limits number between 0 and 1 |
| 85 | + const limited = Math.min(Math.max(corrected, 0), 1); |
| 86 | + |
| 87 | + return Math.round(limited * 255); |
| 88 | +} |
0 commit comments