Skip to content

Commit

Permalink
Added mode for finding color between two colors.
Browse files Browse the repository at this point in the history
  • Loading branch information
marvin19 committed Feb 29, 2024
1 parent a70d19f commit f052b34
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/components/CardGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { generateAdditionalColors } from '../utils';
import FullAccessiblePalette from './FullAccessisblePalette';
import RadioButtonList from './RadioButtonList';
import SectionTitle from './SectionTitle';
import FindColor from './FindColor';

const CardGrid: React.FC = () => {
// You need to have two colors to check contrast
Expand Down Expand Up @@ -146,13 +147,20 @@ const CardGrid: React.FC = () => {
visibleColors={visibleColors}
paletteType={'Adjacent'}
/>
) : (
) : paletteType === 'FullAccessible' ? (
<FullAccessiblePalette
colors={colors}
selectedContrast={selectedContrast}
visibleColors={visibleColors}
paletteType={'FullAccessible'}
/>
) : (
<FindColor
colors={colors}
selectedContrast={selectedContrast}
visibleColors={visibleColors}

Check failure on line 161 in src/components/CardGrid.tsx

View workflow job for this annotation

GitHub Actions / build

Type 'number' is not assignable to type 'string[]'.
paletteType={'FindColor'}
/>
)}
</div>
</div>
Expand Down
56 changes: 56 additions & 0 deletions src/components/FindColor.tsx
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,

Check failure on line 14 in src/components/FindColor.tsx

View workflow job for this annotation

GitHub Actions / build

'visibleColors' is declared but its value is never read.
paletteType,

Check failure on line 15 in src/components/FindColor.tsx

View workflow job for this annotation

GitHub Actions / build

'paletteType' is declared but its value is never read.
}) => {
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

View workflow job for this annotation

GitHub Actions / lint

React Hook useEffect has a missing dependency: 'colors'. Either include it or remove the dependency array

Check warning on line 34 in src/components/FindColor.tsx

View workflow job for this annotation

GitHub Actions / lint

React Hook useEffect has a complex expression in the dependency array. Extract it to a separate variable so it can be statically checked

Check warning on line 34 in src/components/FindColor.tsx

View workflow job for this annotation

GitHub Actions / lint

React Hook useEffect has a complex expression in the dependency array. Extract it to a separate variable so it can be statically checked

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;
10 changes: 10 additions & 0 deletions src/components/RadioButtonList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ const RadioButtonList: React.FC<RadioButtonListProps> = ({
setPaletteType('BackgroundColor');
},
},
{
id: 'findColor',
name: 'paletteType',
value: 'FindColor',
label: 'Find Color between two colors',
checked: paletteType === 'FindColor',
onChange: () => {
setPaletteType('FindColor');
},
},
];

return (
Expand Down

0 comments on commit f052b34

Please sign in to comment.