Skip to content

Add more built-in colors #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
878 changes: 853 additions & 25 deletions src/ColorUtils.ts

Large diffs are not rendered by default.

49 changes: 35 additions & 14 deletions src/lineChecks/checkTagColors.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ParsingContext } from "../ParsingContext";
import { HUMAN_COLORS, hexToRgb, COLORS } from "../ColorUtils";
import { hexToRgb, HUMAN_COLORS_MAP, HTML_COLORS_MAP, MARKWHEN_COLORS } from "../ColorUtils";
import { TAG_COLOR_REGEX } from "../regex";
import { RangeType } from "../Types";

@@ -11,19 +11,8 @@ export function checkTagColors(
): boolean {
const tagColorMatch = line.match(TAG_COLOR_REGEX);
if (tagColorMatch) {
const tagName = tagColorMatch[1];
const colorDef = tagColorMatch[2];
const humanColorIndex = HUMAN_COLORS.indexOf(colorDef);
if (humanColorIndex === -1) {
const rgb = hexToRgb(colorDef);
if (rgb) {
context.tags[tagName] = rgb;
} else {
context.tags[tagName] = COLORS[context.paletteIndex++ % COLORS.length];
}
} else {
context.tags[tagName] = COLORS[humanColorIndex];
}
const tagName = tagColorMatch.groups!.tagName;

const indexOfTag = line.indexOf(tagName);
const from = lengthAtIndex[i] + indexOfTag - 1;
context.ranges.push({
@@ -41,6 +30,37 @@ export function checkTagColors(
content: { tag: tagName, color: context.tags[tagName] },
});

// One of the following is cases is guaranteed to be true based on how TAG_COLOR_REGEX is structured.
if (tagColorMatch.groups!.color_named) {
const colorDef = tagColorMatch.groups!.color_named.toLowerCase();
if (HUMAN_COLORS_MAP.has(colorDef)) {
context.tags[tagName] = HUMAN_COLORS_MAP.get(colorDef)!.rgb;
} else {
// Named color is not recognized. Default to the next markwhen color.
context.tags[tagName] = MARKWHEN_COLORS[context.paletteIndex++ % MARKWHEN_COLORS.length].rgb;
}
}
if (tagColorMatch.groups!.color_html) {
const colorDef = tagColorMatch.groups!.color_html.toLowerCase();
if (HTML_COLORS_MAP.has(colorDef)) {
context.tags[tagName] = HTML_COLORS_MAP.get(colorDef)!.rgb;
} else {
// Named color is not recognized. Default to the next markwhen color.
context.tags[tagName] = MARKWHEN_COLORS[context.paletteIndex++ % MARKWHEN_COLORS.length].rgb;
}
}
if (tagColorMatch.groups!.color_hex) {
const colorDef = tagColorMatch.groups!.color_hex;
const rgb = hexToRgb(colorDef);
if (rgb) {
context.tags[tagName] = rgb;
} else {
// Named color is not recognized. Default to the next markwhen color.
context.tags[tagName] = MARKWHEN_COLORS[context.paletteIndex++ % MARKWHEN_COLORS.length].rgb;
}
}

const colorDef = tagColorMatch.groups!.colorDef;
const indexOfColorDefPlusLength = line.indexOf(colorDef) + colorDef.length;
context.ranges.push({
type: RangeType.tagDefinition,
@@ -56,6 +76,7 @@ export function checkTagColors(
},
content: { tag: tagName, color: context.tags[tagName] },
});

return true;
}
return false;
4 changes: 2 additions & 2 deletions src/lineChecks/checkTags.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ParsingContext } from "../ParsingContext";
import { COLORS } from "../ColorUtils";
import { MARKWHEN_COLORS } from "../ColorUtils";
import { TAG_REGEX } from "../regex";
import { RangeType } from "../Types";

@@ -13,7 +13,7 @@ export function checkTags(
if (matches) {
for (let m of matches) {
if (!context.tags[m[1]]) {
context.tags[m[1]] = COLORS[context.paletteIndex++ % COLORS.length];
context.tags[m[1]] = MARKWHEN_COLORS[context.paletteIndex++ % MARKWHEN_COLORS.length].rgb;
}
const indexOfTag = line.indexOf("#" + m[1]);
const from = lengthAtIndex[i] + indexOfTag;
2 changes: 1 addition & 1 deletion src/regex.ts
Original file line number Diff line number Diff line change
@@ -234,7 +234,7 @@ export const recurrence_recurrenceAmountXNotationAmountMatchIndex = ++index;
export const eventTextMatchIndex = ++index

export const COMMENT_REGEX = /^\s*\/\/.*/;
export const TAG_COLOR_REGEX = /^\s*#(\w*):\s*(\S+)/;
export const TAG_COLOR_REGEX = /^\s*#(?<tagName>\w*):\s*(?<colorDef>(?<html>html\([A-Za-z]+\))|(?<hex>#?[0-9a-fA-F]{6})|(?<named>[A-Za-z]+))/;
export const TITLE_REGEX = /^\s*(title:)\s*(.+)\s*$/i;
export const VIEWERS_REGEX = /^\s*(view:)\s*(.*)$/i;
export const EDITORS_REGEX = /^\s*(edit:)\s*(.*)$/i;