Skip to content

Commit ebbd4ec

Browse files
committed
refactor: dynamically adjust text color contrast based on background lightness using HSL and configurable thresholds.
1 parent 881d6ba commit ebbd4ec

2 files changed

Lines changed: 9 additions & 10 deletions

File tree

src/NodeCanvas.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9898,7 +9898,7 @@ function NodeCanvas() {
98989898
fontWeight: 'bold',
98999899
color: isNodeGroup
99009900
? getTextColor(nodeGroupColor)
9901-
: (getTextColor(strokeColor) === '#262626' ? '#262626' : strokeColor),
9901+
: (hexToHsl(strokeColor).l > 50 ? getTextColor(strokeColor) : strokeColor),
99029902
backgroundColor: 'transparent',
99039903
border: 'none',
99049904
outline: 'none',
@@ -9912,7 +9912,7 @@ function NodeCanvas() {
99129912
<text x={labelX + labelWidth / 2} y={labelY + labelHeight * 0.7 - 2} fontFamily="EmOne, sans-serif" fontSize={fontSize}
99139913
fill={isNodeGroup
99149914
? getTextColor(nodeGroupColor)
9915-
: (getTextColor(strokeColor) === '#262626' ? '#262626' : strokeColor)
9915+
: (hexToHsl(strokeColor).l > 50 ? getTextColor(strokeColor) : strokeColor)
99169916
} fontWeight="bold" stroke={isNodeGroup ? "none" : "#bdb5b5"} strokeWidth={isNodeGroup ? 0 : strokeWidth} paintOrder="stroke fill" textAnchor="middle">{labelText}</text>
99179917
)}
99189918
</g>

src/utils/colorUtils.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
/**
2-
* Shared color utility functions for Redstring
3-
*/
1+
const LIGHTNESS_THRESHOLD = 42;
2+
const DARK_TEXT_LIGHTNESS = 16;
43

54
// Helper function to convert CSS color names to hex
65
export const cssColorToHex = (color) => {
@@ -133,11 +132,11 @@ export const getTextColor = (backgroundColor) => {
133132

134133
const { h, s, l } = hexToHsl(backgroundColor);
135134

136-
// If background is bright (lightness > 35), use dark text with same hue
137-
// Threshold 35% provides good contrast for this UI's specific style
138-
if (l > 35) {
139-
// Use "near black" for better legibility on light backgrounds, as requested
140-
return '#262626';
135+
// If background is bright, use dark text with same hue
136+
if (l > LIGHTNESS_THRESHOLD) {
137+
// Create a dark color with the same hue (preserving saturation) but very low lightness
138+
// This creates a "near black" that matches the theme of the group/node
139+
return hslToHex(h, s, DARK_TEXT_LIGHTNESS);
141140
} else {
142141
// Use light text for dark backgrounds
143142
return '#bdb5b5';

0 commit comments

Comments
 (0)