UNPKG

quasar-framework

Version:

Simultaneously build desktop/mobile SPA websites & phone/tablet apps with VueJS

131 lines (114 loc) 2.33 kB
export function rgbToHex ({ r, g, b, a }) { const alpha = a !== void 0 r = Math.round(r) g = Math.round(g) b = Math.round(b) if ( r > 255 || g > 255 || b > 255 || (alpha && a > 100) ) { throw new TypeError('Expected 3 numbers below 256 (and optionally one below 100)') } a = alpha ? (Math.round(255 * a / 100) | 1 << 8).toString(16).slice(1) : '' return '#' + ((b | g << 8 | r << 16) | 1 << 24).toString(16).slice(1) + a } export function hexToRgb (hex) { if (typeof hex !== 'string') { throw new TypeError('Expected a string') } hex = hex.replace(/^#/, '') if (hex.length === 3) { hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2] } else if (hex.length === 4) { hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2] + hex[3] + hex[3] } let num = parseInt(hex, 16) return hex.length > 6 ? {r: num >> 24 & 255, g: num >> 16 & 255, b: num >> 8 & 255, a: Math.round((num & 255) / 2.55)} : {r: num >> 16, g: num >> 8 & 255, b: num & 255} } export function hsvToRgb ({ h, s, v, a }) { let r, g, b, i, f, p, q, t s = s / 100 v = v / 100 h = h / 360 i = Math.floor(h * 6) f = h * 6 - i p = v * (1 - s) q = v * (1 - f * s) t = v * (1 - (1 - f) * s) switch (i % 6) { case 0: r = v g = t b = p break case 1: r = q g = v b = p break case 2: r = p g = v b = t break case 3: r = p g = q b = v break case 4: r = t g = p b = v break case 5: r = v g = p b = q break } return { r: Math.round(r * 255), g: Math.round(g * 255), b: Math.round(b * 255), a } } export function rgbToHsv ({ r, g, b, a }) { let max = Math.max(r, g, b), min = Math.min(r, g, b), d = max - min, h, s = (max === 0 ? 0 : d / max), v = max / 255 switch (max) { case min: h = 0 break case r: h = (g - b) + d * (g < b ? 6 : 0) h /= 6 * d break case g: h = (b - r) + d * 2 h /= 6 * d break case b: h = (r - g) + d * 4 h /= 6 * d break } return { h: Math.round(h * 360), s: Math.round(s * 100), v: Math.round(v * 100), a } }