random colors now being sampled from hsluv (#80)

This samples colors from the HSLUV color space instead of the RGB color space, which should result in better randomized colors.
pull/84/head
mittimithai 2021-04-06 17:33:08 -07:00 zatwierdzone przez GitHub
rodzic 47d9550e51
commit 558fa0aa6a
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
4 zmienionych plików z 40 dodań i 2 usunięć

1
desc.d.ts vendored 100644
Wyświetl plik

@ -0,0 +1 @@
declare module "colorspaces"

Wyświetl plik

@ -1,5 +1,6 @@
import { Random } from "./random";
import { range } from "./util";
import * as colorspaces from 'colorspaces';
/**
* Clamp the given number to be between 0 and 255, then
@ -19,8 +20,38 @@ export function clampedByteToHex(value: number): string {
}
function createRandomColor(rng: Random): string {
const rgb = range(3).map(() => rng.inRange({ min: 0, max: 255, step: 1 }));
return "#" + rgb.map(clampedByteToHex).join("");
const max_luv_samples = 100;
let luv_sample_failed = true;
let rand_color_hex:string = "#000000";
//See if we can pull out a sample inside the LUV solid
for (let i=0; i<max_luv_samples; i++) {
let L = rng.inRange({ min: 0, max: 100, step: 1 });
let u = rng.inRange({ min: -128, max: 128, step: 1 });
let v = rng.inRange({ min: -128, max: 128, step: 1 });
let rand_color = colorspaces.make_color('CIELUV', [L, u, v]);
if(rand_color.is_displayable()) {
rand_color_hex = rand_color.as('hex');
luv_sample_failed = false;
break;
}
}
//just sample sRGB if I couldn't sample a random LUV color
if(luv_sample_failed) {
console.log("Sampling sRGB")
let rgb = (new Array<number>(3)).map(() => rng.inRange({ min: 0, max: 255, step: 1 }));
console.log(rgb)
for(let i=0; i<rgb.length; i++) {
rgb[i] = rgb[i]/255.0;
}
console.log(rgb)
let rand_color = colorspaces.make_color('sRGB',rgb);
rand_color_hex = rand_color.as('hex');
}
return rand_color_hex;
}
/**

5
package-lock.json wygenerowano
Wyświetl plik

@ -2834,6 +2834,11 @@
"resolved": "https://registry.npmjs.org/colorette/-/colorette-1.2.1.tgz",
"integrity": "sha512-puCDz0CzydiSYOrnXpz/PKd69zRrribezjtE9yd4zvytoRc8+RY/KJPvtPFKZS3E3wP6neGyMe0vOTlHO5L3Pw=="
},
"colorspaces": {
"version": "0.1.5",
"resolved": "https://registry.npmjs.org/colorspaces/-/colorspaces-0.1.5.tgz",
"integrity": "sha1-elqa71MGmN4IkJ2F7QAAf/jDHvk="
},
"combined-stream": {
"version": "1.0.8",
"resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz",

Wyświetl plik

@ -26,6 +26,7 @@
"babel-jest": "^26.6.3",
"cheerio": "^1.0.0-rc.5",
"classnames": "^2.3.1",
"colorspaces": "^0.1.5",
"gh-pages": "^3.1.0",
"jest": "^26.6.3",
"parcel-bundler": "^1.12.4",