Skip to content

Commit

Permalink
even distribution!
Browse files Browse the repository at this point in the history
  • Loading branch information
mcnuttandrew committed Feb 21, 2024
1 parent 87b7a55 commit a677759
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 154 deletions.
1 change: 1 addition & 0 deletions LintLanguageDocs.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Math Operations:
{"/": {left: Number | Variable, right: Number | Variable}}
{"-": {left: Number | Variable, right: Number | Variable}}
{absDiff: {left: Number | Variable, right: Number | Variable}}
{"%": {left: Number | Variable, right: Number | Variable}}

Value Comparisons:
{dist: {left: Color | Variable, right: Color | Variable}, space: COLOR_SPACE }
Expand Down
28 changes: 22 additions & 6 deletions public/lint-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@
{
"additionalProperties": false,
"properties": {
"filter": {"$ref": "#/definitions/alias-1448761563-3658-3719-1448761563-0-6076"},
"filter": {"$ref": "#/definitions/alias-1448761563-3695-3756-1448761563-0-6113"},
"func" : {"$ref": "#/definitions/LintExpression"} ,
"varb" : {"type": "string"}
},
Expand All @@ -395,7 +395,7 @@
"additionalProperties": false,
"properties": {
"func": {"$ref": "#/definitions/LintValue"} ,
"map" : {"$ref": "#/definitions/alias-1448761563-3658-3719-1448761563-0-6076"},
"map" : {"$ref": "#/definitions/alias-1448761563-3695-3756-1448761563-0-6113"},
"varb": {"type": "string"}
},
"required": ["map", "func", "varb"],
Expand All @@ -404,7 +404,7 @@
{
"additionalProperties": false,
"properties": {
"reverse": {"$ref": "#/definitions/alias-1448761563-3658-3719-1448761563-0-6076"}
"reverse": {"$ref": "#/definitions/alias-1448761563-3695-3756-1448761563-0-6113"}
},
"required": ["reverse"],
"type": "object"
Expand All @@ -413,15 +413,15 @@
"additionalProperties": false,
"properties": {
"func": {"$ref": "#/definitions/LintValue"} ,
"sort": {"$ref": "#/definitions/alias-1448761563-3658-3719-1448761563-0-6076"},
"sort": {"$ref": "#/definitions/alias-1448761563-3695-3756-1448761563-0-6113"},
"varb": {"type": "string"}
},
"required": ["sort", "func", "varb"],
"type": "object"
},
{
"additionalProperties": false,
"properties": { "speed": {"$ref": "#/definitions/alias-1448761563-3658-3719-1448761563-0-6076"} },
"properties": { "speed": {"$ref": "#/definitions/alias-1448761563-3695-3756-1448761563-0-6113"} },
"required": ["speed"],
"type": "object"
}
Expand Down Expand Up @@ -492,6 +492,22 @@
},
"required": ["/"],
"type": "object"
},
{
"additionalProperties": false,
"properties": {
"%": {
"additionalProperties": false,
"properties": {
"left" : {"$ref": "#/definitions/LintValue"},
"right": {"$ref": "#/definitions/LintValue"}
},
"required": ["left", "right"],
"type": "object"
}
},
"required": ["%"],
"type": "object"
}
]
},
Expand Down Expand Up @@ -702,7 +718,7 @@
"description": "A LintValue is a JSON object that represents a value. It can be a string, a number, a boolean, a LintColor, a LintVariable, a LintMathOps, a LintPairOps, a LintAggregate, a LintColorFunction or a LintExpression"
},
"LintVariable": {"type": "string"},
"alias-1448761563-3658-3719-1448761563-0-6076": {
"alias-1448761563-3695-3756-1448761563-0-6113": {
"anyOf": [
{"$ref": "#/definitions/LintVariable"} ,
{ "items": {"$ref": "#/definitions/LintValue"}, "type": "array" },
Expand Down
14 changes: 9 additions & 5 deletions src/content-modules/Browse.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
while ($examplePalStore.palettes.length === 0) {
await new Promise((resolve) => setTimeout(resolve, 100));
}
computeLints();
computeLints(false);
});
function computeLints() {
function computeLints(breakLint: boolean) {
$examplePalStore.palettes.forEach(async (pal, index) => {
if (pal.lints.length > 0) {
if (!breakLint && pal.lints.length > 0) {
return;
}
lint(pal.palette, false).then((res) => {
Expand All @@ -37,8 +37,9 @@
fail: "",
warn: "⚠️",
};
$: pals = $examplePalStore.palettes;
$: allEvaluatedLints = $examplePalStore.palettes.reduce(
$: allEvaluatedLints = pals.reduce(
(acc, pal) => {
pal.lints.forEach((lint) => {
acc[lint.name] = lint;
Expand All @@ -49,7 +50,7 @@
);
let disAllowedLints = new Set<string>();
$: filteredPals = $examplePalStore.palettes;
$: filteredPals = pals;
let sortedBy = "natural";
$: console.log(allEvaluatedLints, disAllowedLints);
</script>
Expand All @@ -75,6 +76,9 @@
>
reset filters
</button>
<button class={buttonStyle} on:click={() => computeLints(true)}>
Re run lints
</button>
</div>
<div class="flex flex-col flex-wrap max-h-80">
{#each Object.values(allEvaluatedLints) as lint}
Expand Down
53 changes: 40 additions & 13 deletions src/lib/lint-language/lint-language.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ export class LLNumber extends LLNode {
}
}

const LLNumberOpTypes = ["+", "-", "*", "/", "absDiff"] as const;
const LLNumberOpTypes = ["+", "-", "*", "/", "absDiff", "%"] as const;
export class LLNumberOp extends LLNode {
constructor(
private type: (typeof LLNumberOpTypes)[number],
Expand All @@ -355,16 +355,22 @@ export class LLNumberOp extends LLNode {
return { result: left / right, env };
case "absDiff":
return { result: Math.abs(left - right), env };
case "%":
return { result: left % right, env };
}
}
static tryToConstruct(node: any, options: OptionsConfig) {
const opType = LLNumberOpTypes.find((x) => node[x]);
if (!opType) return false;
const { left, right } = node[opType];
if (!checkIfValsPresent(node[opType], ["left", "right"])) return false;
if (!checkIfValsPresent(node[opType], ["left", "right"])) {
throw new Error(`Missing left or right for ${opType}`);
}
const leftType = tryTypes([LLValue], options)(left);
const rightType = tryTypes([LLValue], options)(right);
if (!leftType || !rightType) return false;
if (!leftType || !rightType) {
throw new Error(`Type error in ${opType}`);
}
return new LLNumberOp(opType, leftType, rightType);
}
toString(): string {
Expand Down Expand Up @@ -401,7 +407,7 @@ function compareValues(
}
return diff < thresh;
}
throw new Error("Type error");
throw new Error("Type error. Similar must be used with colors.");
case "==":
return left === right;
case "!=":
Expand All @@ -412,6 +418,8 @@ function compareValues(
return left < right;
}
}
const getType = (x: any) =>
typeof x === "object" ? (Array.isArray(x) ? "Array" : "object") : typeof x;
export class LLPredicate extends LLNode {
constructor(
public type: (typeof predicateTypes)[number],
Expand All @@ -427,8 +435,13 @@ export class LLPredicate extends LLNode {
const { left, right } = this;
let leftEval = left.evaluate(env).result;
let rightEval = right.evaluate(env).result;
if (typeof leftEval !== typeof rightEval) {
throw new Error("predicate - type error ");
const leftType = getType(leftEval);
const rightType = getType(rightEval);
if (leftType !== rightType) {
throw new Error(
`Type error on predicate "${this.type}": left and right types must be the same.
Got ${leftType} and ${rightType}`
);
}
// array
if (Array.isArray(leftEval)) {
Expand Down Expand Up @@ -523,7 +536,7 @@ const VFTypes = [
primaryKey: "toColor",
params: ["space", "channel"] as string[],
op: (val: Color, params: Params) =>
val.toColorSpace(params.space as any).getChannel(params.channel),
Number(val.toColorSpace(params.space as any).getChannel(params.channel)),
},
{
primaryKey: "inGamut",
Expand All @@ -539,7 +552,7 @@ Object.entries(colorPickerConfig).map(([colorSpace, value]) => {
primaryKey: `${colorSpace}.${channelKey.toLowerCase()}`,
params: [] as string[],
op: (val: Color, _params: Params) =>
val.toColorSpace(colorSpace as any).getChannel(channelKey),
Number(val.toColorSpace(colorSpace as any).getChannel(channelKey)),
});
});
});
Expand All @@ -558,7 +571,9 @@ export class LLValueFunction extends LLNode {
// get the value of the input, such as by deref
const inputEval = input.evaluate(env).result;
if (!(inputEval instanceof Color)) {
throw new Error("Type error");
throw new Error(
`Type error, was expecting a color, but got ${inputEval} in function ${this.type}`
);
}
const op = VFTypes.find((x) => x.primaryKey === this.type);
if (!op) throw new Error("Invalid type");
Expand Down Expand Up @@ -886,7 +901,9 @@ export class LLAggregate extends LLNode {
} else {
childType = tryTypes([LLVariable, LLMap], options)(children);
}
if (!childType) return false;
if (!childType) {
throw new Error(`Type error in ${reduceType}`);
}
return new LLAggregate(reduceType, childType);
}
toString(): string {
Expand Down Expand Up @@ -927,8 +944,13 @@ export class LLMap extends LLNode {
case "filter":
return { result: children.filter(evalFunc), env };
case "sort":
const childrenCopy = [...children].map(evalFunc) as RawValues[];
childrenCopy.sort();
let childrenCopy = [...children].map(evalFunc) as RawValues[];
childrenCopy = childrenCopy.sort((a, b) => {
if (typeof a === "number" && typeof b === "number") {
return a - b;
}
return a.toString().localeCompare(b.toString());
});
return { result: childrenCopy, env };
case "reverse":
const childrenCopy2 = [...children];
Expand Down Expand Up @@ -982,7 +1004,12 @@ export class LLMap extends LLNode {
options
)(node.func);
}
if (!func || !childType || !varb) return false;
if (!func || !childType || !varb) {
throw new Error(
`Failed while parsing ${op}. func=${func}, child=${childType}, varb=${varb}`
);
return false;
}
return new LLMap(op, childType, func, varb);
}
toString(): string {
Expand Down
3 changes: 2 additions & 1 deletion src/lib/lint-language/lint-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ export type LintMathOps =
| { "+": { left: LintValue; right: LintValue } }
| { "-": { left: LintValue; right: LintValue } }
| { "*": { left: LintValue; right: LintValue } }
| { "/": { left: LintValue; right: LintValue } };
| { "/": { left: LintValue; right: LintValue } }
| { "%": { left: LintValue; right: LintValue } };

export type LintPairOps =
| LintPairOpsDist
Expand Down
3 changes: 2 additions & 1 deletion src/lib/linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import { CreateCustomLint } from "./CustomLint";

// manual lints
import DivergingOrder from "./lints/diverging-order";
import EvenDistribution from "./lints/even-distribution";

// custom lints
import Affects from "./lints/affects";
import AvoidExtremes from "./lints/avoid-extremes";
import BackgroundDifferentiability from "./lints/background-contrast";
import CatOrderSimilarity from "./lints/cat-order-similarity";
import ColorBlindness from "./lints/color-blindness";
import EvenDistribution from "./lints/even-distribution";
import Fair from "./lints/fair";
import Gamut from "./lints/in-gamut";
import MaxColors from "./lints/max-colors";
Expand All @@ -30,6 +30,7 @@ export const BUILT_INS: CustomLint[] = [
AvoidExtremes,
BackgroundDifferentiability,
CatOrderSimilarity,
EvenDistribution,
Gamut,
MaxColors,
MutuallyDistinct,
Expand Down
Loading

0 comments on commit a677759

Please sign in to comment.