import {hsbToRgb, rgbToHsb} from './rgb-hsb' import {ColorRGB} from './common' const NEUTRAL_SATURATION_LIST = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34] const NEUTRAL_BRIGHTNESS_LIST = [100, 98, 96, 93, 88, 80, 60, 40, 20, 0] const COLOR_BASE_INDEX = 5 export function rgbNeutralLevel(color: ColorRGB, index: number) { const h = Math.round(rgbToHsb(color).h) const b = NEUTRAL_BRIGHTNESS_LIST[index] const s = h === 0 ? 0 : NEUTRAL_SATURATION_LIST[index] return hsbToRgb({h, s, b}) } function hueLevel(h: number, index: number) { const step = 1 h = Math.round(h) h = h - (index - COLOR_BASE_INDEX) * step return (360 + h) % 360 } function saturationLevel(s: number, index: number) { s = Math.round(s) const step = index < COLOR_BASE_INDEX ? Math.round(s / 5) : 5 s = s + (index - COLOR_BASE_INDEX) * step return s > 100 ? 100 : Math.max(s, 5) } function brightnessLevel(b: number, index: number) { b = Math.round(b) const step = index < COLOR_BASE_INDEX ? -5 : -15 b = b + (index - COLOR_BASE_INDEX) * step return b > 100 ? 100 : Math.max(b, 0) } export function rgbLevel(color: ColorRGB, index: number) { if (index === COLOR_BASE_INDEX) { return color } const {h, s, b} = rgbToHsb(color) return hsbToRgb({ h: hueLevel(h, index), s: saturationLevel(s, index), b: brightnessLevel(b, index), }) }