Skip to content

Commit

Permalink
fix colors
Browse files Browse the repository at this point in the history
  • Loading branch information
karsonkalt committed Jul 9, 2024
1 parent fafcd49 commit 29f8b23
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
2 changes: 1 addition & 1 deletion assets/js/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion assets/js/terminal.js

Large diffs are not rendered by default.

37 changes: 21 additions & 16 deletions assets/ts/terminal/updateAccentColor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const updateAccentColor = (inputColor: string): boolean => {

const accentColor = getAdjustedAccentColor(chroma(inputColor));
const accentColorContrast = getContrastColor(accentColor);
const accentDecoration = isLightColor(accentColorContrast)
const accentDecoration = hasLowSaturation(accentColorContrast)
? "underline"
: "none";

Expand All @@ -31,8 +31,8 @@ const getAdjustedAccentColor = (color: chroma.Color): chroma.Color => {
const luminance = color.luminance();
if (luminance < 0.25) {
color = color.luminance(0.25);
} else if (luminance > 0.75) {
color = color.luminance(0.75);
} else if (luminance > 0.5) {
color = color.luminance(0.5);
}

// Ensure color is vibrant but not fully saturated
Expand All @@ -41,32 +41,35 @@ const getAdjustedAccentColor = (color: chroma.Color): chroma.Color => {
};

const getContrastColor = (accentColor: chroma.Color): chroma.Color => {
// Generate a fully vibrant version of the accent color
let vibrantColor = accentColor.set("hsl.s", 1);
// Check if the color is totally grey
if (accentColor.get("hsl.s") === 0) {
// Return a vibrant link-friendly color like teal blue
const colorOptions = ["#00796b", "#00acc1", "#1976d2", "#2196f3"];
accentColor = chroma(
colorOptions[Math.floor(Math.random() * colorOptions.length)]
);
}

const vibrantColor = accentColor.set("hsl.s", 1);
vibrantColor.set("hsl.l", 0.75);

const MIN_CONTRAST = 1.75;

// Ensure the vibrant color meets the 3 contrast ratio against the accent color
if (
chroma.contrast(vibrantColor, accentColor) >= MIN_CONTRAST
// && chroma.contrast(vibrantColor, "black") >= 4.5
) {
if (chroma.contrast(vibrantColor, accentColor) >= MIN_CONTRAST) {
return vibrantColor;
}

// Adjust the lightness to find a suitable contrast color
const lightnessSteps = 10;
const lightnessSteps = 20;
const lightnessIncrement = 1 / lightnessSteps;

for (let i = 1; i <= lightnessSteps; i++) {
const lightenedColor = vibrantColor.set(
"hsl.l",
vibrantColor.get("hsl.l") + lightnessIncrement * i
);
if (
chroma.contrast(lightenedColor, accentColor) >= MIN_CONTRAST
// && chroma.contrast(lightenedColor, "black") >= 4.5
) {
if (chroma.contrast(lightenedColor, accentColor) >= MIN_CONTRAST) {
return lightenedColor;
}
}
Expand All @@ -75,8 +78,10 @@ const getContrastColor = (accentColor: chroma.Color): chroma.Color => {
return vibrantColor;
};

const isLightColor = (color: chroma.Color): boolean => {
return color.luminance() > 0.75;
const hasLowSaturation = (color: chroma.Color): boolean => {
const saturation = color.get("hsl.s");
// Adjusting the condition to focus on low saturation to include all shades of grey and white.
return saturation < 0.2;
};

const setColorsInLocalStorage = (
Expand Down

0 comments on commit 29f8b23

Please sign in to comment.