feat(ui): color picker final touches (#491)

* fix: update CourseCellColorPicker.tsx background to white

* feat: add color picker to CalendarCourseCell component

* feat: add color picker functionality to update course colors

* fix: type issues with storybook components

* feat: add useColorPicker hook, isValidHexColor and updateCourseColors utilities

* refactor: color picker logic and UI components

* refactor: update useFlattenedCourseSchedule hook to include courseID property

* refactor: update storybook calendar components with updated props

* refactor: update color picker ui logic to account for position of cell

* fix: revert back to error handling for invalid rgb

* refactor: update jsdocs

* refactor: integrate ColorPickerContext into Calendar components and update props

* refactor: integrate ColorPickerContext into Calendar components and update related props

* refactor: change JSDocs comments and remove unused color inversion state

* refactor: update story components

* feat: add functionality for selecting secondary course colors

* refactor: enhance HexColorEditor to dynamically adjust tag icon color based on preview color

* refactor: simplify JSDoc comment in useColorPicker hook

* fix: revert Button component

* refactor: update CalendarCourseCell component positioning and styling

* fix: correct types in color.ts

* feat: add getDarkerShade function to compute darker shades of hex colors

* feat: add shadow to color picker button

* fix: update button size in ColorPatch component

* feat: implement debounced input for hex color editor and add useDebounce hook

* chore: utilize the logical and && operator instead of the ternary operator

* fix: imports and palette icon

* refactor: remove unused import

* fix: bug when course add fails with custom colors

* chore: run lint

* chore: run check-types

* feat: add HSL color type and conversion functions

* refactor: rename colorway to theme

* fix: hide color picker on screenshot

* fix: undo important syntax

* refactor: rename SomeFunction to DebouncedCallback

* refactor: remove inner function

* refactor: update return type to DebouncedCallback

* fix: adjust sizes for hash and palette button

* feat: create tests for hexToHSL and isValidHexColor

* refactor: update parameter type to use HexColor

* fix: increase size of palette button

* fix: update dependency array for hex code debounce

* fix: change colorPickerRef element ref

* feat: add Roboto Mono font

* fix: update input class to use monospace font

* feat: add getLighterShade function

* chore: run prettier and lint

* feat: synchronize local hex code with hexCode prop changes

---------

Co-authored-by: doprz <52579214+doprz@users.noreply.github.com>
This commit is contained in:
Ethan Lanting
2025-01-29 20:32:06 -06:00
committed by GitHub
parent aa29bcf9fe
commit c2328e461e
7 changed files with 82 additions and 4 deletions

Binary file not shown.

View File

@@ -264,6 +264,33 @@ export function getDarkerShade(color: HexColor, offset: number = 20): HexColor {
return `#${newRGB.map(c => Math.round(c).toString(16).padStart(2, '0')).join('')}`;
}
/**
* Returns a lighter shade of the given hex color by increasing the lightness in HSL color space.
*
* @param color - The hexadecimal color value to lighten.
* @param offset - The percentage to increase the lightness by (default is 20).
* @returns The lighter shade of the given hex color.
* @throws If the provided color is not a valid hex color.
*/
export function getLighterShade(color: HexColor, offset: number = 20): HexColor {
const rgb = hexToRGB(color);
if (!rgb) {
throw new Error('color: Invalid hex.');
}
// Convert to HSL
const [h, s, l] = hexToHSL(color);
// Increase lightness by offset percentage, ensuring it doesn't go above 100
const newL = Math.min(100, l + offset);
// Convert back to RGB
const newRGB = hslToRGB([h, s, newL]);
// Convert to hex
return `#${newRGB.map(c => Math.round(c).toString(16).padStart(2, '0')).join('')}`;
}
/**
* Get next unused color in a tailwind colorway for a given schedule
*

View File

@@ -1,4 +1,4 @@
import { hexToHSL, isValidHexColor } from '@shared/util/colors';
import { getLighterShade, hexToHSL, isValidHexColor } from '@shared/util/colors';
import { describe, expect, it } from 'vitest';
describe('hexToHSL', () => {
@@ -79,3 +79,34 @@ describe('isValidHexColor', () => {
expect(isValidHexColor('')).toBe(false);
});
});
describe('getLighterShade', () => {
it('should lighten a color by default offset (20%)', () => {
const result = getLighterShade('#BF5700');
expect(result).toBe('#ff8624');
});
it('should lighten black correctly', () => {
const result = getLighterShade('#000000');
expect(result).toBe('#333333');
});
it('should not exceed 100% lightness', () => {
const result = getLighterShade('#FFFFFF', 20);
expect(result).toBe('#ffffff');
});
it('should handle custom offset values', () => {
const result = getLighterShade('#BF5700', 40);
expect(result).toBe('#ffbe8a');
});
it('should maintain hue while increasing lightness', () => {
const result = getLighterShade('#00FF00', 20); // Pure green
expect(result.toLowerCase()).toBe('#66ff66');
});
it('should throw error for invalid hex color', () => {
expect(() => getLighterShade('#GGGGGG')).toThrow('color: Invalid hex.');
});
});

View File

@@ -29,6 +29,12 @@ export default function HexColorEditor({ hexCode, setHexCode }: HexColorEditorPr
const [localHexCode, setLocalHexCode] = React.useState(hexCode);
const debouncedSetHexCode = useDebounce((value: string) => setHexCode(value), 500);
React.useEffect(() => {
if (hexCode !== localHexCode) {
setLocalHexCode(hexCode);
}
}, [hexCode]);
React.useEffect(() => {
debouncedSetHexCode(localHexCode);
@@ -48,7 +54,7 @@ export default function HexColorEditor({ hexCode, setHexCode }: HexColorEditorPr
<input
type='text'
maxLength={6}
className='w-full border-none bg-transparent font-size-2.75 font-normal outline-none focus:outline-none'
className='w-full border-none bg-transparent font-size-2.75 font-normal font-mono outline-none focus:outline-none'
value={localHexCode}
onChange={e => setLocalHexCode(e.target.value)}
/>

View File

@@ -1,7 +1,7 @@
import { UserScheduleStore } from '@shared/storage/UserScheduleStore';
import type { HexColor } from '@shared/types/Color';
import { UserSchedule } from '@shared/types/UserSchedule';
import { getColorwayFromColor, getCourseColors, getDarkerShade } from '@shared/util/colors';
import { getColorwayFromColor, getCourseColors, getDarkerShade, getLighterShade } from '@shared/util/colors';
import { useEffect, useState } from 'react';
let schedulesCache: UserSchedule[] = [];
@@ -150,7 +150,12 @@ export async function updateCourseColors(courseID: number, primaryColor: HexColo
secondaryColor = colorFromWay;
} catch (e) {
secondaryColor = getDarkerShade(primaryColor, 80);
secondaryColor = getDarkerShade(primaryColor, 20);
// if primaryColor is too dark, get lighter shade instead
if (secondaryColor === '#000000') {
secondaryColor = getLighterShade(primaryColor, 35);
}
}
updatedCourse.colors.primaryColor = primaryColor;

View File

@@ -15,7 +15,15 @@
font-style: normal;
}
@font-face {
font-family: 'Roboto Mono Local';
src: url('@public/fonts/roboto-mono.woff2') format('woff2');
font-display: swap;
font-style: normal;
}
@import url('https://fonts.googleapis.com/css2?family=Roboto+Flex:wght@100..1000&display=swap');
@import url('https://fonts.googleapis.com/css2?family=Roboto+Mono&display=swap');
$medium_size: 16px;

View File

@@ -55,6 +55,7 @@ export default defineConfig({
provider: 'none',
fonts: {
sans: ['Roboto Flex', 'Roboto Flex Local'],
mono: ['Roboto Mono', 'Roboto Mono Local'],
},
}),
],