Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 | 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x 1x 1x 8x 8x 8x 8x 8x 1x 1x 1x 1x 1x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 1x 1x 1x 825x 825x 825x 825x 825x 1x 1x 1x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 4x 4x 4x 4x 4x 4x 4x 56x 56x 4x 4x 4x 3x 3x 3x 4x 1x 4x 56x 56x 56x 56x 56x 56x 4x 4x 4x 4x 4x 4x 4x 4x 4x 56x 56x 56x 56x 56x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 56x 56x 56x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 19x 19x 19x 19x 19x 2x 2x 2x 1x 2x 2x 2x 8x 8x 8x 8x 8x 8x 2x 1x 1x 1x 9x 9x 9x 9x 9x 9x 9x 9x 9x 1x 1x 1x 2x 56x 56x 2x 2x 2x 56x 56x 56x 2x 2x 2x 56x 56x 56x 56x 1x 1x 8x 8x 1x 1x 1x 1x 1x 1x 1x 48x 48x 41x 4x 4x 29x 17x 17x 41x 10x 10x 10x 10x 41x 48x 1x 1x 75x 75x 75x 75x 75x 34x 75x 75x 75x 1x 1x 75x 75x 1x 1x 17x 17x 1x 1x 60x 60x 1x 1x 11x 11x 1x 1x 66x 66x 1x 1x 1x 1x 1x 1x 66x 66x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 11x 11x 11x 11x 11x 11x 11x 1x 1x 1x 1x 1x 1x 1x 267x 267x 267x 267x 267x 267x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 73x 73x 73x 73x 73x 73x 1x 1x 1x 1x 1x 1x 132x 132x 132x 132x 132x 132x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import { calculateDistanceBetween2Points, CircleInfo } from './geometry.js' import { registerElement, registerCSSProperties } from './util.js' import html from './color-wheel.element.html' import css from './color-wheel.element.css' let loadTemplate = () => { const templateElement = document.createElement('template') templateElement.innerHTML = html loadTemplate = () => templateElement return templateElement } let loadStyles = () => { const sheet = new CSSStyleSheet() sheet.replaceSync(css) loadStyles = () => sheet return sheet } /** * * @param {TransitionEvent} event - property transition event */ function uiModeTransitionEventHandler (event) { const { target } = event if (!(target instanceof HTMLElement)) return const rootNode = target.getRootNode() if (!(rootNode instanceof ShadowRoot)) return const { host } = rootNode if (host instanceof ColorWheelElement) { updateContainerUIModeClass(host) } } /** @type {(shadowRoot: ParentNode | null, selector: string) => HTMLElement } */ const queryRequired = (shadowRoot, selector) => { if (!shadowRoot) { throw new Error(`Color-wheel: Error from shadowDOM: parent node is ${shadowRoot}`) } const result = shadowRoot.querySelector(selector) if (!result || !(result instanceof HTMLElement)) { throw new Error(`Color-wheel: Error from shadowDOM: html element "${selector}" not found`) } return result } class ColorWheelElement extends HTMLElement { constructor () { super() const shadowRoot = this.attachShadow({ mode: 'open' }) shadowRoot.adoptedStyleSheets = [loadStyles()] const template = loadTemplate() shadowRoot.append(document.importNode(template.content, true)) const fromShadowRoot = queryRequired.bind(null, shadowRoot) const wheelContainer = fromShadowRoot('.color-wheel-container') const innerRadiusCalc = fromShadowRoot('.inner-radius-calc') const wheel = fromShadowRoot('.color-wheel') const slider = fromShadowRoot('.slider') if (!wheelContainer || !innerRadiusCalc) { throw new Error('Error loading Color-wheel: ".color-wheel-container", ".inner-radius-calc", ".color-wheel" or not found') } const wheelStyle = window.getComputedStyle(wheel) updateContainerUIModeClass(this) reflectLightness(this) reflectValue(this) getContainer(this).addEventListener('transitionstart', uiModeTransitionEventHandler) const getWheelCenterPoint = () => { const pointerBox = wheelContainer.getBoundingClientRect() const centerPoint = wheelStyle.transformOrigin const centers = centerPoint.split(' ') const centerY = pointerBox.top + parseInt(centers[1]) const centerX = pointerBox.left + parseInt(centers[0]) return { x: centerX, y: centerY } } const getRadiusValues = () => { const pointerBox = wheelContainer.getBoundingClientRect() const innerRadiusCSSValue = wheelStyle.getPropertyValue('--inner-radius').trim() if (/[0-9]+%/g.test(innerRadiusCSSValue)) { const innerRadiusPerc = parseFloat(innerRadiusCSSValue) return CircleInfo.fromRectWithPercentInnerRadius(pointerBox, innerRadiusPerc) } const innerRadius = innerRadiusCalc.getBoundingClientRect().width return CircleInfo.fromRectWithInnerRadius(pointerBox, innerRadius) } /** * Inits drag event, calling `callback` until it finishes, preventing default event and propagation during the operation * @param {(event: PointerEvent) => void} callback - callback to execute */ const initDrag = (callback) => { const window = this.ownerDocument.defaultView if (!window) { return } /** @param {PointerEvent} e - event to pass to `callback` */ const defaultPrevented = e => { e.preventDefault(); e.stopPropagation(); callback(e) } window.addEventListener('pointermove', defaultPrevented, { capture: true }) window.addEventListener('pointerup', () => { window.removeEventListener('pointermove', defaultPrevented, { capture: true }) }, { once: true, capture: true }) } /** * Init slider drag and drop */ const initSliderDrag = () => { const centerPoint = getWheelCenterPoint() const disk = getRadiusValues() /** @param {PointerEvent} e - pointermove event */ const slideSaturation = (e) => { const saturationPoint = { x: centerPoint.x, y: Math.min(centerPoint.y, e.clientY), } const newSaturation = calculateSaturation(disk, saturationPoint) this.saturation = newSaturation const event = new CustomEvent('input', { bubbles: true }) this.dispatchEvent(event) } initDrag(slideSaturation) } /** @param {PointerEvent} pointerEvent - pointerdown event */ const initWheelDrag = (pointerEvent) => { const centerPoint = getWheelCenterPoint() const disk = getRadiusValues() const { hue } = this /** * @param {PointerEvent} e - pointermove event * @returns {number} theta value in degrees */ const getAngle = (e) => { const deltaX = e.clientX - centerPoint.x const deltaY = centerPoint.y - e.clientY const thetaRadians = Math.atan2(deltaY, deltaX) return thetaRadians * -180 / Math.PI } const initDeg = getAngle(pointerEvent) const uiMode = this.uiMode if ((uiMode || '').trim() === 'mobile') { /** @param {PointerEvent} e - pointermove event */ const rotateWheel = (e) => { const deg = getAngle(e) const newHue = Math.round(deg - initDeg + hue + 360) % 360 this.hue = newHue const event = new CustomEvent('input', { bubbles: true }) this.dispatchEvent(event) } initDrag(rotateWheel) } else { /** @param {PointerEvent} e - pointermove event */ const rotateSlider = (e) => { const deg = getAngle(e) const newHue = Math.round(-deg + 360 * 2 - 90) % 360 this.hue = newHue const clientPoint = { x: e.clientX, y: e.clientY } const newSaturation = calculateSaturation(disk, clientPoint) this.saturation = newSaturation const event = new CustomEvent('input', { bubbles: true }) this.dispatchEvent(event) } rotateSlider(pointerEvent) initDrag(rotateSlider) } } wheel.addEventListener('pointerdown', (event) => { event.preventDefault() event.stopPropagation() initWheelDrag(/** @type {PointerEvent} */ (event)) }) slider.addEventListener('pointerdown', (event) => { event.preventDefault() event.stopPropagation() initSliderDrag() }) reflectHue(this) reflectSaturation(this) } static get observedAttributes () { return ['saturation', 'hue', 'lightness', 'value'] } /** * @param {string} name - changed attribute name * @param {string} oldValue - old attribute value * @param {string} newValue - new attribute value */ attributeChangedCallback (name, oldValue, newValue) { if (oldValue === newValue) return switch (name) { case 'saturation': reflectSaturation(this) return case 'hue': reflectHue(this) return case 'lightness': reflectLightness(this) return case 'value': reflectValue(this) } } get uiMode () { const computedStyle = getComputedStyle(getContainer(this)) const uiMode = computedStyle.getPropertyValue('--ui-mode').trim() switch (uiMode) { case 'desktop': case 'mobile': return uiMode } return computedStyle.getPropertyValue('--default-ui-mode').trim() } get hue () { return getNumericValueFromAttribute(this, 'hue', 0) } set hue (hue) { this.setAttribute('hue', String(hue)) } get saturation () { return getNumericValueFromAttribute(this, 'saturation', 0) } set saturation (saturation) { this.setAttribute('saturation', String(saturation)) } get value () { return getNumericValueFromAttribute(this, 'value', 100) } set value (value) { this.setAttribute('value', String(value)) } get lightness () { return getNumericValueFromAttribute(this, 'lightness', 50) } set lightness (lightness) { this.setAttribute('lightness', String(lightness)) } } /** * @param {import('./geometry.js').Disk} disk - color wheel geometry * @param {import('./geometry.js').Point} point - target position * @returns {number} saturation value, will not go below 0 or above 100 */ function calculateSaturation (disk, point) { const { center, radius, innerRadiusPerc } = disk const r = calculateDistanceBetween2Points(center, point) const rperc = Math.min(100, Math.max(0, r * 100 / radius)) const saturation = (rperc - innerRadiusPerc) * 100 / (100 - innerRadiusPerc) return Math.round(Math.min(100, Math.max(0, saturation))) } /** * @param {ColorWheelElement} element - target Element * @param {string} attribute - attribute * @param {number} defaultValue - default value * @returns {number} - numeric value, or default if not defined or invalid */ function getNumericValueFromAttribute (element, attribute, defaultValue) { const value = element.getAttribute(attribute) if (!value) { return defaultValue } const asInt = parseInt(value) return isNaN(asInt) ? defaultValue : asInt } /** * @param {ColorWheelElement} element - target element * @returns {HTMLElement} container element */ const getContainer = element => queryRequired(element.shadowRoot, '.container') /** * @param {ColorWheelElement} element - target element * @param {string} property - css * @param {string | number} value - new valuw */ const setContainerProperty = (element, property, value) => { getContainer(element).style.setProperty(property, String(value)) } /** * * @param {ColorWheelElement} element - target element */ function updateContainerUIModeClass (element) { const container = getContainer(element) const { uiMode } = element container.classList.toggle('container--desktop-ui', uiMode === 'desktop') container.classList.toggle('container--mobile-ui', uiMode === 'mobile') } /** * updates ColorWheelElement color view based on the data * By default it used the HSV color scheme, it only uses HSL when only HSL attributes are defined * @param {ColorWheelElement} element - target element */ function reflectHsl (element) { const container = getContainer(element) if (!container) { return } const setHSLMode = element.hasAttribute('lightness') && !element.hasAttribute('value') container.classList.toggle('container--hsl', setHSLMode) } /** @param {ColorWheelElement} element - target element */ const reflectHue = element => { setContainerProperty(element, '--hue', element.hue) } /** @param {ColorWheelElement} element - target element */ const reflectSaturation = element => { setContainerProperty(element, '--saturation', element.saturation) } /** @param {ColorWheelElement} element - target element */ const reflectLightness = element => { reflectHsl(element); setContainerProperty(element, '--lightness', element.lightness) } /** @param {ColorWheelElement} element - target element */ const reflectValue = element => { reflectHsl(element); setContainerProperty(element, '--value', element.value) } const url = new URL(import.meta.url) const elementName = url.searchParams.get('named') elementName && registerElement(elementName, ColorWheelElement) registerCSSProperties() |