Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit e218f6d

Browse files
committed
Changed rainbow algorithm
1 parent 177b76d commit e218f6d

File tree

2 files changed

+87
-67
lines changed

2 files changed

+87
-67
lines changed

src/utils/colour.js

Lines changed: 0 additions & 67 deletions
This file was deleted.

src/utils/colour.ts

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
import { number } from "prop-types";
18+
19+
export function textToHtmlRainbow(str: string): string {
20+
const frequency = (2 * Math.PI) / str.length;
21+
22+
return Array.from(str)
23+
.map((c, i) => {
24+
if (c === " ") {
25+
return c;
26+
}
27+
const [a, b] = generateAB(i * frequency, 1);
28+
const [red, green, blue] = labToRGB(75, a, b);
29+
return (
30+
'<font color="#' +
31+
red.toString(16).padStart(2, "0") +
32+
green.toString(16).padStart(2, "0") +
33+
blue.toString(16).padStart(2, "0") +
34+
'">' +
35+
c +
36+
"</font>"
37+
);
38+
})
39+
.join("");
40+
}
41+
42+
function generateAB(hue: number, chroma: number): [number, number] {
43+
const a = chroma * 127 * Math.cos(hue);
44+
const b = chroma * 127 * Math.sin(hue);
45+
46+
return [a, b];
47+
}
48+
49+
function labToRGB(l: number, a: number, b: number): [number, number, number] {
50+
// Convert CIELAB to CIEXYZ (D65)
51+
var y = (l + 16) / 116;
52+
const x = adjustXYZ(y + a / 500) * 0.9505;
53+
const z = adjustXYZ(y - b / 200) * 1.089;
54+
55+
y = adjustXYZ(y);
56+
57+
// Linear transformation from CIEXYZ to RGB
58+
const red = 3.24096994 * x - 1.53738318 * y - 0.49861076 * z;
59+
const green = -0.96924364 * x + 1.8759675 * y + 0.04155506 * z;
60+
const blue = 0.05563008 * x - 0.20397696 * y + 1.05697151 * z;
61+
62+
return [adjustRGB(red), adjustRGB(green), adjustRGB(blue)];
63+
}
64+
65+
function adjustXYZ(v: number): number {
66+
if (v > 0.2069) {
67+
return Math.pow(v, 3);
68+
}
69+
return 0.1284 * v - 0.01771;
70+
}
71+
72+
function gammaCorrection(v: number): number {
73+
// Non-linear transformation to sRGB
74+
if (v <= 0.0031308) {
75+
return 12.92 * v;
76+
}
77+
return 1.055 * Math.pow(v, 1 / 2.4) - 0.055;
78+
}
79+
80+
function adjustRGB(v: number): number {
81+
const corrected = gammaCorrection(v);
82+
83+
// Limits number between 0 and 1
84+
const limited = Math.min(Math.max(corrected, 0), 1);
85+
86+
return Math.round(limited * 255);
87+
}

0 commit comments

Comments
 (0)