@@ -8,17 +8,17 @@ import Debouncer from "../../utils/debouncer";
88import FNote from "../../entities/fnote" ;
99import froca from "../../services/froca" ;
1010
11- const COLORS = [
11+ const COLOR_PALETTE = [
1212 "#e64d4d" , "#e6994d" , "#e5e64d" , "#99e64d" , "#4de64d" , "#4de699" ,
1313 "#4de5e6" , "#4d99e6" , "#4d4de6" , "#994de6" , "#e64db3"
1414] ;
1515
16- export interface NoteColorPickerMenuItemProps {
16+ export interface NoteColorPickerProps {
1717 /** The target Note instance or its ID string. */
1818 note : FNote | string | null ;
1919}
2020
21- export default function NoteColorPickerMenuItem ( props : NoteColorPickerMenuItemProps ) {
21+ export default function NoteColorPicker ( props : NoteColorPickerProps ) {
2222 if ( ! props . note ) return null ;
2323
2424 const [ note , setNote ] = useState < FNote | null > ( null ) ;
@@ -43,22 +43,15 @@ export default function NoteColorPickerMenuItem(props: NoteColorPickerMenuItemPr
4343 useEffect ( ( ) => {
4444 const colorLabel = note ?. getLabel ( "color" ) ?. value ?? null ;
4545 if ( colorLabel ) {
46- let color : ColorInstance | null = null ;
47-
48- try {
49- color = new Color ( colorLabel ) ;
50- } catch ( ex ) {
51- console . error ( ex ) ;
52- }
53-
46+ let color = tryParseColor ( colorLabel ) ;
5447 if ( color ) {
5548 setCurrentColor ( color . hex ( ) . toLowerCase ( ) ) ;
5649 }
5750 }
5851 } , [ note ] ) ;
5952
6053 useEffect ( ( ) => {
61- setIsCustomColor ( currentColor !== null && COLORS . indexOf ( currentColor ) === - 1 ) ;
54+ setIsCustomColor ( currentColor !== null && COLOR_PALETTE . indexOf ( currentColor ) === - 1 ) ;
6255 } , [ currentColor ] )
6356
6457 const onColorCellClicked = useCallback ( ( color : string | null ) => {
@@ -84,7 +77,7 @@ export default function NoteColorPickerMenuItem(props: NoteColorPickerMenuItemPr
8477 onSelect = { onColorCellClicked } />
8578
8679
87- { COLORS . map ( ( color ) => (
80+ { COLOR_PALETTE . map ( ( color ) => (
8881 < ColorCell key = { color }
8982 tooltip = { t ( "note-color.set-color" ) }
9083 color = { color }
@@ -155,7 +148,7 @@ function CustomColorCell(props: ColorCellProps) {
155148 colorInput . current ?. click ( ) ;
156149 } , [ pickedColor ] ) ;
157150
158- return < div style = { `--foreground: ${ ensureContrast ( props . color ) } ;` } >
151+ return < div style = { `--foreground: ${ getForegroundColor ( props . color ) } ;` } >
159152 < ColorCell { ...props }
160153 color = { pickedColor }
161154 className = { `custom-color-cell ${ ( pickedColor === null ) ? "custom-color-cell-empty" : "" } ` }
@@ -170,17 +163,24 @@ function CustomColorCell(props: ColorCellProps) {
170163 </ div >
171164}
172165
173- function ensureContrast ( color : string | null ) {
174- if ( color === null ) return "inherit" ;
175-
176- const colorHsl = Color ( color ) . hsl ( ) ;
177- let l = colorHsl . lightness ( ) ;
166+ function getForegroundColor ( backgroundColor : string | null ) {
167+ if ( backgroundColor === null ) return "inherit" ;
178168
179- if ( l >= 40 ) {
180- l = 0 ;
169+ const colorHsl = tryParseColor ( backgroundColor ) ?. hsl ( ) ;
170+ if ( colorHsl ) {
171+ let l = colorHsl . lightness ( ) ;
172+ return colorHsl . saturationl ( 0 ) . lightness ( l >= 50 ? 0 : 100 ) . hex ( ) ;
181173 } else {
182- l = 100
174+ return "inherit" ;
175+ }
176+ }
177+
178+ function tryParseColor ( colorStr : string ) : ColorInstance | null {
179+ try {
180+ return new Color ( colorStr ) ;
181+ } catch ( ex ) {
182+ console . error ( ex ) ;
183183 }
184184
185- return colorHsl . saturationl ( 0 ) . lightness ( l ) . hex ( ) ;
185+ return null ;
186186}
0 commit comments