조도계로 노출을 잡는 식 EV = log₂(E·S / C) 에 센서의 lux(E)를 넣는다 — S 는 ISO, C 는 입사광 노출계 교정 상수(평판 수광부 250).1

async function main({ onActivate, onError, onReading, onDenied }) {
  const result = await navigator.permissions.query({
    name: 'ambient-light-sensor',
  })

  if (result.state === 'denied') {
    onDenied()
    return
  }

  const ambientLightSensor = new AmbientLightSensor({ frequency: 20 })
  
  ambientLightSensor.addEventListener('activate', onActivate)
  ambientLightSensor.addEventListener('error', onError)
  ambientLightSensor.addEventListener('reading', () => {
    const ISO = 100
    const C = 250

    const EV = Math.round(Math.log2((ambientLightSensor.illuminance * ISO) / C))

    onReading(EV)
  })

  ambientLightSensor.start()
}


Footnotes

  1. Exposure value - Wikipedia: EV = log₂(ES/C) (E 조도, C 입사광 노출계 교정 상수) / Light meter - Wikipedia: 평판 수광부 C 는 “a value of 250 is commonly used.” ↩

  2. 주변광 센서 인터페이스가 포함된 일반 센서 API를 사용하는 방법을 설명 ↩

#257