pull/208/head
Maneesh Yadav 2021-08-29 12:10:47 -07:00
rodzic 8de2f078d3
commit 4ac2050ae0
2 zmienionych plików z 127 dodań i 19 usunięć

Wyświetl plik

@ -101,11 +101,11 @@ function angleFromOrigin(p: Point) {
return Math.atan2(p.y, p.x); return Math.atan2(p.y, p.x);
} }
/*
function lengthOfRayUntilIntersect(theta: Angle, l: Line) { function lengthOfRayUntilIntersect(theta: Angle, l: Line) {
return l.intercept / (Math.sin(theta) - l.slope * Math.cos(theta)); return l.intercept / (Math.sin(theta) - l.slope * Math.cos(theta));
} }
*/
function getBounds(L: number): Line[] { function getBounds(L: number): Line[] {
let result: Line[] = []; let result: Line[] = [];
@ -128,8 +128,8 @@ function getBounds(L: number): Line[] {
return result; return result;
} }
/*
function maxSafeChromaForL(L): number { export function maxSafeChromaForL(L): number {
let bounds = getBounds(L); let bounds = getBounds(L);
let min = Infinity; let min = Infinity;
@ -141,7 +141,8 @@ function maxSafeChromaForL(L): number {
return min; return min;
} }
function maxChromaForLH(L: number, H: number): number {
export function maxChromaForLH(L: number, H: number): number {
let hrad = (H / 360) * Math.PI * 2; let hrad = (H / 360) * Math.PI * 2;
let bounds = getBounds(L); let bounds = getBounds(L);
let min = Infinity; let min = Infinity;
@ -156,7 +157,7 @@ function maxChromaForLH(L: number, H: number): number {
return min; return min;
} }
/*
function dotProduct(a, b) { function dotProduct(a, b) {
let sum = 0; let sum = 0;
for (let i = 0; i < a.length; i++) for (let i = 0; i < a.length; i++)

Wyświetl plik

@ -3,7 +3,7 @@ import { range, clamp } from "./util";
import * as colorspaces from "colorspaces"; import * as colorspaces from "colorspaces";
import { ColorTuple, luvToLch, lchToHsluv, hsluvToHex } from "hsluv"; import { ColorTuple, luvToLch, lchToHsluv, hsluvToHex } from "hsluv";
import { clampedBytesToRGBColor } from "./color-util"; import { clampedBytesToRGBColor } from "./color-util";
import { getUVSliceGeometery } from "./chromatools"; import { getUVSliceGeometery, maxSafeChromaForL, maxChromaForLH } from "./chromatools";
type RandomPaletteGenerator = (numEntries: number, rng: Random) => string[]; type RandomPaletteGenerator = (numEntries: number, rng: Random) => string[];
//type ColorFunction = (rng: Random) => string[]; //type ColorFunction = (rng: Random) => string[];
@ -13,10 +13,16 @@ export type RandomPaletteAlgorithm =
| "CIELUV" | "CIELUV"
| "threevals" | "threevals"
| "randhue" | "randhue"
| "hichroma"; | "randLAlmostNeutralC"
| "randLMinSafeC"
| "randLMaxSafeC"
| "midLMinSafeC"
| "midLMaxC"
| "chromaContrast"
| "hiChroma";
export const DEFAULT_RANDOM_PALETTE_ALGORITHM: RandomPaletteAlgorithm = export const DEFAULT_RANDOM_PALETTE_ALGORITHM: RandomPaletteAlgorithm =
"hichroma"; "hiChroma";
function createRandomRGBColor(rng: Random): string { function createRandomRGBColor(rng: Random): string {
const rgb = range(3).map(() => rng.inRange({ min: 0, max: 255, step: 1 })); const rgb = range(3).map(() => rng.inRange({ min: 0, max: 255, step: 1 }));
@ -128,6 +134,67 @@ function clampHue(h: number): number {
return hn; return hn;
} }
function getMixedRGBs(hsluvs: ColorTuple[],rng: Random): ColorTuple[] {
hsluvs = rng.uniqueChoices(hsluvs, hsluvs.length);
let hexcolors = hsluvs.map((x) => hsluvToHex(x as ColorTuple));
return hexcolors;
}
function randLAlmostNeutralC(rng: Random): string[] {
let L = rng.inInterval({ min: 10, max: 90 });
let hsluvs: ColorTuple[] = [];
let C = maxSafeChromaForL(L) * 0.2;
let h = rng.inInterval({min: 0, max: 360});
for (let i = 0; i < 3; i++) {
let lch = [L,C,clampHue(h+i*120)];
let hsluv = lchToHsluv(lch);
hsluvs.push(hsluv);
}
return getMixedRGBs(hsluvs,rng);
}
function randLMinSafeC(rng: Random): string[] {
let L = rng.inInterval({ min: 10, max: 90 });
let hsluvs: ColorTuple[] = [];
let C = maxSafeChromaForL(L);
let h = rng.inInterval({min: 0, max: 360});
for (let i = 0; i < 3; i++) {
let lch = [L,C,clampHue(h+i*120)];
let hsluv = lchToHsluv(lch);
hsluvs.push(hsluv);
}
return getMixedRGBs(hsluvs,rng);
}
function randLMaxSafeC(rng: Random): string[] {
let L = rng.inInterval({ min: 10, max: 90 });
let hs: number[] = [];
let Cs: number[] = [];
let hsluvs: ColorTuple[] = [];
let h = rng.inInterval({min: 0, max: 360});
for (let i = 0; i < 3; i++) {
let h_i = clampHue(h+i*120)
hs.push(h_i);
Cs.push(maxChromaForLH(L, h_i));
}
let C = Math.min( ...Cs );
for (let i = 0; i < 3; i++) {
let lch = [L,C,hs[i]];
let hsluv = lchToHsluv(lch);
hsluvs.push(hsluv);
}
return getMixedRGBs(hsluvs,rng);
}
function hiChroma(rng: Random): string[] { function hiChroma(rng: Random): string[] {
let L1 = rng.inInterval({ min: 0, max: 25 }); let L1 = rng.inInterval({ min: 0, max: 25 });
let L2 = rng.inInterval({ min: L1 + 25, max: 60 }); let L2 = rng.inInterval({ min: L1 + 25, max: 60 });
@ -148,17 +215,51 @@ function hiChroma(rng: Random): string[] {
hsluvs.push(hsluv); hsluvs.push(hsluv);
} }
//randomize return getMixedRGBs(hsluvs,rng);
hsluvs = rng.uniqueChoices(hsluvs, hsluvs.length); }
//occasionally desaturate the bg function midLMinSafeC(rng: Random): string[] {
if (rng.bool(0.5)) { let L = 50;
hsluvs[0][1] = hsluvs[0][1] * 0.1; let hsluvs: ColorTuple[] = [];
let C = maxSafeChromaForL(L);
for (let i = 0; i < 3; i++) {
let lch = [L,C,rng.inInterval({min: 0, max: 360})];
let hsluv = lchToHsluv(lch);
hsluvs.push(hsluv);
} }
console.log("Maneesh");
console.log(hsluvs[0]); return getMixedRGBs(hsluvs,rng);
let hexcolors = hsluvs.map((x) => hsluvToHex(x as ColorTuple)); }
return hexcolors;
function midLMaxC(rng: Random): string[] {
let L = 50;
let hs: number[] = [];
let hsluvs: ColorTuple[] = [];
//let h = rng.inInterval({min: 0, max: 360});
for (let i = 0; i < 3; i++) {
let hsluv = [rng.inInterval({min: 0, max: 360}) ,100,L];
hsluvs.push(hsluv);
}
return getMixedRGBs(hsluvs,rng);
}
function chromaContrast(rng: Random): string[] {
let L = rng.inInterval({ min: 10, max: 90 });
let h = rng.inInterval({min: 0, max: 360});
let Ss = [20,60,100];
let hsluvs: ColorTuple[] = [];
for (let i = 0; i < 3; i++) {
let hsluv = [h,Ss[i],L];
hsluvs.push(hsluv);
}
return getMixedRGBs(hsluvs,rng);
} }
/** /**
@ -202,7 +303,13 @@ const PALETTE_GENERATORS: {
CIELUV: createSimplePaletteGenerator(createRandomCIELUVColor), CIELUV: createSimplePaletteGenerator(createRandomCIELUVColor),
threevals: createTriadPaletteGenerator(threeVColor), threevals: createTriadPaletteGenerator(threeVColor),
randhue: createTriadPaletteGenerator(randHue), randhue: createTriadPaletteGenerator(randHue),
hichroma: createTriadPaletteGenerator(hiChroma), randLAlmostNeutralC: createTriadPaletteGenerator(randLAlmostNeutralC),
randLMinSafeC: createTriadPaletteGenerator(randLMinSafeC),
randLMaxSafeC: createTriadPaletteGenerator(randLMaxSafeC),
midLMinSafeC: createTriadPaletteGenerator(midLMinSafeC),
midLMaxC: createTriadPaletteGenerator(midLMaxC),
chromaContrast: createTriadPaletteGenerator(chromaContrast),
hiChroma: createTriadPaletteGenerator(hiChroma)
}; };
export const RANDOM_PALETTE_ALGORITHMS = Object.keys( export const RANDOM_PALETTE_ALGORITHMS = Object.keys(