-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added mode for finding color between two colors.
- Loading branch information
Showing
3 changed files
with
75 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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,56 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
import { calculateContrastRatio, getRgb, rgbToHex } from '../utils'; | ||
|
||
interface FindColorProps { | ||
selectedContrast: number; | ||
colors: string[]; | ||
visibleColors: string[]; | ||
paletteType: string; | ||
} | ||
|
||
const FindColor: React.FC<FindColorProps> = ({ | ||
colors, | ||
selectedContrast, | ||
visibleColors, | ||
paletteType, | ||
}) => { | ||
const [contrastColor, setContrastColor] = useState<string | null>(null); | ||
|
||
useEffect(() => { | ||
let newColor: string | null = null; | ||
for (let i = 0; i < 1000; i++) { | ||
newColor = rgbToHex(getRgb(), getRgb(), getRgb()); | ||
if ( | ||
calculateContrastRatio(newColor, colors[0]) >= | ||
selectedContrast && | ||
calculateContrastRatio(newColor, colors[1]) >= selectedContrast | ||
) { | ||
break; | ||
} else { | ||
newColor = null; | ||
} | ||
} | ||
setContrastColor(newColor); | ||
}, [colors[0], colors[1], selectedContrast]); | ||
Check warning on line 34 in src/components/FindColor.tsx GitHub Actions / lint
Check warning on line 34 in src/components/FindColor.tsx GitHub Actions / lint
|
||
|
||
return ( | ||
<div> | ||
{contrastColor !== null ? ( | ||
<> | ||
<div | ||
style={{ | ||
width: '50px', | ||
height: '50px', | ||
backgroundColor: contrastColor, | ||
}} | ||
/> | ||
<p>Contrast color: {contrastColor}</p> | ||
</> | ||
) : ( | ||
'No colors available with enough contrast.' | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
export default FindColor; |
This file contains 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