Skip to content

Commit

Permalink
improve name comparison lang support
Browse files Browse the repository at this point in the history
  • Loading branch information
mcnuttandrew committed Mar 5, 2024
1 parent 7bc3a3f commit 319c9dd
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 20 deletions.
10 changes: 5 additions & 5 deletions public/lang-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ Program:

Description: Palettes that seek to be playful should have at least one light blue, beige, or gray. See "Affective color in visualization" for more.

Natural Language: EXIST c in colors, (similar(c, #add8e6) < 20 or similar(c, #f5f5dc) < 20 or similar(c, #808080) < 20)
Natural Language: EXIST c in colors, (similar(c, lightblue) < 20 or similar(c, beige) < 20 or similar(c, gray) < 20)


Program:
Expand Down Expand Up @@ -216,7 +216,7 @@ Program:

Description: Palettes that seek to be positive should not have dark reds or browns. See "Affective color in visualization" for more.

Natural Language: ALL c in colors, NOT (similar(c, #8b0000) < 20 or similar(c, #a52a2a) < 20)
Natural Language: ALL c in colors, NOT (similar(c, darkred) < 20 or similar(c, brown) < 20)


Program:
Expand Down Expand Up @@ -248,7 +248,7 @@ Program:

Description: Palettes that seek to be negative should not have light colors, particularly greens. See "Affective color in visualization" for more.

Natural Language: ALL c in colors, NOT (similar(c, #008000) < 20 or lab.l(c) > 70)
Natural Language: ALL c in colors, NOT (similar(c, green) < 20 or lab.l(c) > 70)


Program:
Expand Down Expand Up @@ -1013,7 +1013,7 @@ Program:

Description: Colors at either end of the lightness spectrum can be hard to discriminate in some contexts, and are sometimes advised against. See https://blog.datawrapper.de/beautifulcolors/#6 for more.

Natural Language: ALL a in colors, ALL b in ([#000, #fff, #00f, #f00, #0f0]), NOT a == b
Natural Language: ALL a in colors, ALL b in ([#000000, #ffffff, #0000ff, #ff0000, #00ff00]), NOT a == b

Palettes that will fail this test:

Expand Down Expand Up @@ -1400,7 +1400,7 @@ Program:

Description: Colors that are close to what are known as ugly colors are sometimes advised against. See https://www.colourlovers.com/palette/1416250/The_Ugliest_Colors for more details.

Natural Language: ALL a in colors, ALL b in ([#56ff00, #0010ff, #6a7e25, #ff00ef, #806e28]), deltaE(a, b, 2000) > 10
Natural Language: ALL a in colors, ALL b in ([#56FF00, #0010FF, #6A7E25, #FF00EF, #806E28]), deltaE(a, b, 2000) > 10

Palettes that will fail this test:

Expand Down
10 changes: 5 additions & 5 deletions src/lib/__snapshots__/LintDocs.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ Program:
Description: Palettes that seek to be playful should have at least one light blue, beige, or gray. See "Affective color in visualization" for more.
Natural Language: EXIST c in colors, (similar(c, #add8e6) < 20 or similar(c, #f5f5dc) < 20 or similar(c, #808080) < 20)
Natural Language: EXIST c in colors, (similar(c, lightblue) < 20 or similar(c, beige) < 20 or similar(c, gray) < 20)
Program:
Expand Down Expand Up @@ -219,7 +219,7 @@ Program:
Description: Palettes that seek to be positive should not have dark reds or browns. See "Affective color in visualization" for more.
Natural Language: ALL c in colors, NOT (similar(c, #8b0000) < 20 or similar(c, #a52a2a) < 20)
Natural Language: ALL c in colors, NOT (similar(c, darkred) < 20 or similar(c, brown) < 20)
Program:
Expand Down Expand Up @@ -251,7 +251,7 @@ Program:
Description: Palettes that seek to be negative should not have light colors, particularly greens. See "Affective color in visualization" for more.
Natural Language: ALL c in colors, NOT (similar(c, #008000) < 20 or lab.l(c) > 70)
Natural Language: ALL c in colors, NOT (similar(c, green) < 20 or lab.l(c) > 70)
Program:
Expand Down Expand Up @@ -1016,7 +1016,7 @@ Program:
Description: Colors at either end of the lightness spectrum can be hard to discriminate in some contexts, and are sometimes advised against. See https://blog.datawrapper.de/beautifulcolors/#6 for more.
Natural Language: ALL a in colors, ALL b in ([#000, #fff, #00f, #f00, #0f0]), NOT a == b
Natural Language: ALL a in colors, ALL b in ([#000000, #ffffff, #0000ff, #ff0000, #00ff00]), NOT a == b
Palettes that will fail this test:
Expand Down Expand Up @@ -1403,7 +1403,7 @@ Program:
Description: Colors that are close to what are known as ugly colors are sometimes advised against. See https://www.colourlovers.com/palette/1416250/The_Ugliest_Colors for more details.
Natural Language: ALL a in colors, ALL b in ([#56ff00, #0010ff, #6a7e25, #ff00ef, #806e28]), deltaE(a, b, 2000) > 10
Natural Language: ALL a in colors, ALL b in ([#56FF00, #0010FF, #6A7E25, #FF00EF, #806E28]), deltaE(a, b, 2000) > 10
Palettes that will fail this test:
Expand Down
18 changes: 16 additions & 2 deletions src/lib/lint-language/LintLanguage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ test("LintLanguage Avoid Extreme Colors", () => {
},
};
expect(prettyPrintLL(program)).toBe(
"ALL a in colors, ALL b in ([#000, #fff]), a != b"
"ALL a in colors, ALL b in ([#000000, #ffffff]), a != b"
);
const { result, blame } = LLEval(program, greens);
expect(result).toBe(true);
Expand Down Expand Up @@ -581,7 +581,7 @@ test("LintLanguage Avoid Extreme Colors Swapped Predicate Order (blame test)", (
},
};
expect(prettyPrintLL(program)).toBe(
"ALL a in ([#000, #fff]), ALL b in colors, a != b"
"ALL a in ([#000000, #ffffff]), ALL b in colors, a != b"
);
const { result, blame } = LLEval(program, greens);
expect(result).toBe(true);
Expand Down Expand Up @@ -837,3 +837,17 @@ test("LintLanguage Fair Weighting", () => {
expect(result.result).toBe(false);
expect(result.blame).toStrictEqual([]);
});

test("LintLanguage Name Check", () => {
const program = {
"==": {
left: { name: "#f00" },
right: "red",
},
};
const astString = prettyPrintLL(program);
expect(astString).toBe("name(#f00) == red");
const result = LLEval(program, toPal([]));
expect(result.result).toBe(true);
expect(result.blame).toStrictEqual([]);
});
28 changes: 20 additions & 8 deletions src/lib/lint-language/lint-language.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ export class LLVariable extends LLNode {
}

export class LLColor extends LLNode {
constructor(private value: Color) {
constructor(private value: Color, private constructorString: string) {
super();
}
evaluate(env: Environment): ReturnVal<Color> {
Expand All @@ -302,15 +302,16 @@ export class LLColor extends LLNode {
}
static tryToConstruct(value: any, options: OptionsConfig): false | LLColor {
if (value instanceof Color) {
return new LLColor(value);
return new LLColor(value, value.toHex());
}
if (typeof value === "string" && Color.stringIsColor(value, "lab")) {
return new LLColor(Color.colorFromString(value, "lab"));
return new LLColor(Color.colorFromString(value, "lab"), value);
}
return false;
}
toString(): string {
return this.value.toHex();
return this.constructorString;
// return this.value.toHex();
}
}

Expand Down Expand Up @@ -423,8 +424,14 @@ function compareValues(
return left < right;
}
}
const getType = (x: any) =>
typeof x === "object" ? (Array.isArray(x) ? "Array" : "object") : typeof x;
const getType = (x: any) => {
if (x instanceof Color) return "Color";
return typeof x === "object"
? Array.isArray(x)
? "Array"
: "object"
: typeof x;
};
export class LLPredicate extends LLNode {
constructor(
public type: (typeof predicateTypes)[number],
Expand All @@ -442,7 +449,12 @@ export class LLPredicate extends LLNode {
let rightEval = right.evaluate(env).result;
const leftType = getType(leftEval);
const rightType = getType(rightEval);
if (leftType !== rightType) {
// allow comparing colors and strings
if (leftType === "string" && rightType === "Color") {
rightEval = right.toString();
} else if (leftType === "Color" && rightType === "string") {
leftEval = left.toString();
} else if (leftType !== rightType) {
throw new Error(
`Type error on predicate "${this.type}": left and right types must be the same.
Got ${leftType} and ${rightType}`
Expand Down Expand Up @@ -535,7 +547,7 @@ const VFTypes = [
{
primaryKey: "name",
params: [] as string[],
op: (val: Color, _params: Params) => getName(val),
op: (val: Color, _params: Params) => getName(val).toLowerCase(),
},
{
primaryKey: "toColor",
Expand Down

0 comments on commit 319c9dd

Please sign in to comment.