색상 선택기

Color picker

정해진 팔레트나 색상환에서 색 하나를 고르는 컨트롤. 스와치 그리드나 네이티브 OS 피커로 만듭니다.

다른 이름: Color swatch pickerSwatch selector
···
html
<div class="cpw">
  <span class="cplabel">강조 색상</span>
  <div class="cpswatches" role="radiogroup" aria-label="색상 선택" id="cpsw">
    <button role="radio" aria-checked="true" aria-label="보라" style="background:#5b5bf7"></button>
    <button role="radio" aria-checked="false" aria-label="핑크" style="background:#f25c8a"></button>
    <button role="radio" aria-checked="false" aria-label="민트" style="background:#18c29c"></button>
    <button role="radio" aria-checked="false" aria-label="주황" style="background:#f2994a"></button>
    <button class="cpcustom" role="radio" aria-checked="false" aria-label="직접 선택"></button>
  </div>
  <div class="cppreview"><span id="cphex">#5B5BF7</span></div>
</div>
css
.cpw{position:absolute;inset:0;display:grid;place-items:center;gap:12px}
.cplabel{color:var(--muted);font-size:10px}
.cpswatches{display:flex;gap:8px}
.cpswatches button{width:26px;height:26px;border-radius:50%;border:2px solid transparent;cursor:default;padding:0}
.cpswatches button[aria-checked=true]{border-color:var(--fg);box-shadow:0 0 0 2px var(--bg)}
.cpcustom{background:conic-gradient(from 0deg,#f25c8a,#f2994a,#e0d454,#18c29c,#5b5bf7,#a35bf7,#f25c8a) !important}
.cppreview{font-size:11px;font-weight:700;color:var(--fg);letter-spacing:.03em}
js
const btns = [...document.querySelectorAll('#cpsw button')];
const hex = document.getElementById('cphex');
const HEX = { 0: '#5B5BF7', 1: '#F25C8A', 2: '#18C29C', 3: '#F2994A', 4: '#A35BF7' };
function select(i) {
  btns.forEach((b, idx) => b.setAttribute('aria-checked', String(idx === i)));
  hex.textContent = HEX[i];
}
let auto = true, i = 0;
btns.forEach((b, idx) => b.addEventListener('click', () => { auto = false; i = idx; select(i); }));
setInterval(() => { if (!auto) return; i = (i + 1) % btns.length; select(i); }, 1100);

네이티브 <input type="color">는 클릭하면 OS의 색상 선택 UI(팔레트, 스포이드)를 그대로 열어주므로 별도 구현 없이도 접근성이 보장됩니다. 브랜드 팔레트처럼 정해진 몇 가지 색 중에서 고르게 하고 싶을 때는 커스텀 스와치 그리드를 role="radiogroup"과 각 스와치 role="radio"로 만들어 한 번에 하나만 선택되게 하고, 방향키로 스와치 사이를 이동하게 합니다.

색만으로 선택 상태나 의미를 전달하면 색맹·저시력 사용자는 구분하지 못합니다(a11y 카테고리의 "색만으로 전달하지 않기" 원칙과 같은 문제). 선택된 스와치에는 색과 별개로 테두리·체크 표시 같은 형태 신호를 더하고, 각 스와치의 aria-label에 "보라", "핑크"처럼 색 이름을 텍스트로 남겨야 스크린리더 사용자도 무엇을 고르는지 압니다.

자유 색상 입력(HEX 코드 타이핑, 색상환 드래그)까지 필요하면 팔레트 옆에 "직접 선택" 스와치를 두고, 눌렀을 때 더 큰 커스텀 피커(대개 HSL이나 채널별 슬라이더)를 여는 구성이 흔합니다.