[Javascript] 배경색에 따라 텍스트 색상 변경하기

0후·2023년 4월 10일
0

프론트엔드

목록 보기
38/41
function getTextColorByBackgroundColor(hexColor) {
    const c = hexColor.substring(1)      // 색상 앞의 # 제거
    const rgb = parseInt(c, 16)   // rrggbb를 10진수로 변환
    const r = (rgb >> 16) & 0xff  // red 추출
    const g = (rgb >>  8) & 0xff  // green 추출
    const b = (rgb >>  0) & 0xff  // blue 추출
    const luma = 0.2126 * r + 0.7152 * g + 0.0722 * b // per ITU-R BT.709
    // 색상 선택
    return luma < 127.5 ? "white" : "black" // 글자색이
}

http://yoonbumtae.com/?p=2977

profile
휘발방지

0개의 댓글