배경색에 맞는 font color 적용
배경색 rgb 구하기 함수
const getTextColorByBackgroundColor = (hexColor: string) => {
const colorToNumber = hexColor.substring(1);
const rgb = parseInt(colorToNumber, 16);
const r = (rgb >> 16) & 0xff;
const g = (rgb >> 8) & 0xff;
const b = (rgb >> 0) & 0xff;
const luma = 0.2126 * r + 0.7152 * g + 0.0722 * b;
if (luma < 127.5) setTitleColor("white");
else setTitleColor("black");
};
styled component로 적용하기
const DetailTitleContainer = styled.div<{ backgroundColor: string }>`
background-color: ${({ backgroundColor }) => backgroundColor};
`;
const DetailTitleText = styled.div<{ titleColor: string }>`
h1 {
color: ${({ titleColor }) => titleColor};
}
`;
return 문 작성
return (
<DetailTitleContainer backgroundColor={backgroundColor}>
<DetailTitleHeader>
{field} {">"} {subCategory}
</DetailTitleHeader>
<DetailTitleText titleColor={titleColor}>
<h1>{title}</h1>
<h3>{subtitle}</h3>
</DetailTitleText>
</DetailTitleContainer>
);