-
Notifications
You must be signed in to change notification settings - Fork 16
Implemented an effective 'dark mode' when tertiary color is too light #586
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| /** | ||
| * Converts a hex color to RGB values | ||
| */ | ||
| export const hexToRgb = (hex: string): { r: number; g: number; b: number } | null => { | ||
| // Remove # if present | ||
| const cleanHex = hex.replace(/^#/, ''); | ||
|
|
||
| // Handle 3-character hex codes | ||
| const fullHex = cleanHex.length === 3 | ||
| ? cleanHex.split('').map(c => c + c).join('') | ||
| : cleanHex; | ||
|
|
||
| const result = /^([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(fullHex); | ||
| return result | ||
| ? { | ||
| r: parseInt(result[1], 16), | ||
| g: parseInt(result[2], 16), | ||
| b: parseInt(result[3], 16), | ||
| } | ||
| : null; | ||
| }; | ||
|
|
||
| /** | ||
| * Normalizes any CSS color value to a hex code using the browser's canvas API | ||
| * Handles hex codes, named colors (e.g., "navy"), rgb(), hsl(), etc. | ||
| */ | ||
| export const normalizeColorToHex = (color: string): string | null => { | ||
| if (!color) return null; | ||
|
|
||
| const trimmed = color.trim(); | ||
| if (!trimmed) return null; | ||
|
|
||
| // Use canvas to parse any CSS color value | ||
| const ctx = document.createElement('canvas').getContext('2d'); | ||
| if (!ctx) return null; | ||
|
|
||
| ctx.fillStyle = trimmed; | ||
| // The browser normalizes the color to a hex string (or rgb() for transparent colors) | ||
| return ctx.fillStyle; | ||
| }; | ||
|
|
||
| /** | ||
| * Calculates the relative luminance of a color | ||
| * Based on WCAG 2.0 formula: https://www.w3.org/TR/WCAG20/#relativeluminancedef | ||
| * Returns a value between 0 (black) and 1 (white) | ||
| */ | ||
| export const getLuminance = (color: string): number => { | ||
| const hex = normalizeColorToHex(color); | ||
| if (!hex) return 0; | ||
|
|
||
| const rgb = hexToRgb(hex); | ||
| if (!rgb) return 0; | ||
|
|
||
| const { r, g, b } = rgb; | ||
|
|
||
| // Convert to sRGB | ||
| const sR = r / 255; | ||
| const sG = g / 255; | ||
| const sB = b / 255; | ||
|
|
||
| // Apply gamma correction | ||
| const R = sR <= 0.03928 ? sR / 12.92 : Math.pow((sR + 0.055) / 1.055, 2.4); | ||
| const G = sG <= 0.03928 ? sG / 12.92 : Math.pow((sG + 0.055) / 1.055, 2.4); | ||
| const B = sB <= 0.03928 ? sB / 12.92 : Math.pow((sB + 0.055) / 1.055, 2.4); | ||
|
|
||
| // Calculate luminance | ||
| return 0.2126 * R + 0.7152 * G + 0.0722 * B; | ||
| }; | ||
|
|
||
| /** | ||
| * Determines if dark mode should be used based on the color | ||
| * Dark mode is enabled when the color is light (luminance > 0.5) | ||
| * This threshold can be adjusted based on needs | ||
| */ | ||
| export const darkMode = (color: string, threshold = 0.5): boolean => { | ||
| return getLuminance(color) > threshold; | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logic: SSR compatibility:
documentis not available in server-side environmentsPrompt To Fix With AI