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
rodzic
47d9550e51
commit
558fa0aa6a
|
@ -0,0 +1 @@
|
|||
declare module "colorspaces"
|
|
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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",
|
||||
|
|
Ładowanie…
Reference in New Issue