Skip to content

Commit

Permalink
Color tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
eirslett committed May 12, 2024
1 parent ce9e976 commit 18a026a
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 4 deletions.
3 changes: 3 additions & 0 deletions frontend/components/ReedPage/ReedPage.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
--border-radius: 8px;
--border-width: 2px;

/*
Alas, this is not supported by all browsers yet. So it's implemented in JS for now.
--clamped-border-color: solid hsl(from var(--reed-color, black) h s clamp(0, l, 0.7));
--clamped-background-color: hsl(from var(--reed-color, black) h s clamp(0.95, l, 1));
*/

position: relative;
border-radius: var(--border-radius);
Expand Down
6 changes: 5 additions & 1 deletion frontend/components/ReedPage/ReedPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
PlayedReedModal,
ScrapedReedModal,
} from './Modals';
import { clampedBackgroundColor, clampedBorderColor } from '../../utils/color';

export function ReedRoute() {
const data = useData();
Expand Down Expand Up @@ -199,9 +200,12 @@ export function ReedPage({
clippedTip: (data: { length: string }) => void;
discard: () => void;
}) {
const colorHex = findColorHex(reed.data.threadColor) ?? '#000000';
// @ts-expect-error - TS doesn't know about CSS custom properties
const style: CSSProperties = {
'--reed-color': findColorHex(reed.data.threadColor),
'--reed-color': colorHex,
'--clamped-border-color': clampedBorderColor(colorHex),
'--clamped-background-color': clampedBackgroundColor(colorHex),
viewTransitionName: 'reed-card', // 'reed-card-' + reed.data.reedIdentification,
};

Expand Down
6 changes: 4 additions & 2 deletions frontend/components/ReedSummary/ReedSummary.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
--border-radius: 8px;
--border-width: 2px;

/*
Alas, this is not supported by all browsers yet. So it's implemented in JS for now.
--clamped-border-color: solid hsl(from var(--reed-color, black) h s clamp(0, l, 0.7));
--clamped-background-color: hsl(from var(--reed-color, black) h s clamp(0.95, l, 1));
*/

position: relative;
border-radius: var(--border-radius);
/* border: var(--border-width) solid transparent; */
padding: var(--padding-size) var(--padding-size) var(--padding-size)
calc(var(--swatch-width) + var(--padding-size));

Expand Down Expand Up @@ -39,7 +41,7 @@
content: '';
inset: 0;
border-radius: var(--border-radius);
border: var(--border-width) var(--clamped-border-color);
border: var(--border-width) solid var(--clamped-border-color);
}

.reed-summary:hover::after {
Expand Down
8 changes: 7 additions & 1 deletion frontend/components/ReedSummary/ReedSummary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { findColorHex } from '../ColorPicker/utils';
import { timestampToLocaleFormattedDate } from '../../utils/date';
import { Link, unstable_useViewTransitionState } from 'react-router-dom';
import { useState } from 'react';
import { clampedBackgroundColor, clampedBorderColor } from '../../utils/color';

function keyValue(key: string, value: string) {
// key: value but with non-breaking space
Expand Down Expand Up @@ -62,9 +63,14 @@ export function ReedSummary({ id, data, lastComment, lastUpdate }: Reed & { id:

const isTransitioning = unstable_useViewTransitionState(to);

const colorHex = findColorHex(data.threadColor) ?? '#000000';
// @ts-expect-error - TS doesn't know about CSS custom properties
const style: CSSProperties = {
'--reed-color': findColorHex(data.threadColor),
'--reed-color': colorHex,

'--clamped-border-color': clampedBorderColor(colorHex),
'--clamped-background-color': clampedBackgroundColor(colorHex),

viewTransitionName: isTransitioning ? 'reed-card' : undefined, // reed-card-' + id,
};

Expand Down
58 changes: 58 additions & 0 deletions frontend/utils/color.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// hsl(from var(--reed-color, black) h s clamp(0, l, 0.7))
// Do the calculation above, but in JS instead of CSS. reedColor is a hex string.
export function clampedBorderColor(reedColorHex: string) {
const hsl = hexToHsl(reedColorHex);
const l = Math.min(hsl[2], 0.7);
return hslToHex([hsl[0], hsl[1], l]);
}

// --clamped-background-color: hsl(from var(--reed-color, black) h s clamp(0.95, l, 1));
// Do the calculation above, but in JS instead of CSS. reedColor is a hex string.
export function clampedBackgroundColor(reedColorHex: string) {
const hsl = hexToHsl(reedColorHex);
const l = Math.max(0.95, hsl[2]);
return hslToHex([hsl[0], hsl[1], l]);
}

function hexToHsl(hex: string) {
const r = parseInt(hex.substring(1, 3), 16) / 255;
const g = parseInt(hex.substring(3, 5), 16) / 255;
const b = parseInt(hex.substring(5, 7), 16) / 255;

const max = Math.max(r, g, b);
const min = Math.min(r, g, b);
const l = (max + min) / 2;

let h = 0;
let s = 0;
if (max !== min) {
const d = max - min;
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
switch (max) {
case r:
h = ((g - b) / d + (g < b ? 6 : 0)) / 6;
break;
case g:
h = ((b - r) / d + 2) / 6;
break;
case b:
h = ((r - g) / d + 4) / 6;
break;
}
}

return [h, s, l];
}

function hslToHex(hsl: [number, number, number]) {
const [h, s, l] = hsl;
const a = s * Math.min(l, 1 - l);
const f = (n: number) => {
const k = (n + h * 12) % 12;
const color = l - a * Math.max(Math.min(k - 3, 9 - k, 1), -1);
return Math.round(255 * color)
.toString(16)
.padStart(2, '0');
};
return `#${f(0)}${f(8)}${f(4)}`;
}

0 comments on commit 18a026a

Please sign in to comment.