vuetify
Version:
Vue Material Component Framework
198 lines • 3.81 kB
JavaScript
// Utilities
import { HexToHSV, HSLtoHSV, HSVtoHex, HSVtoHSL, HSVtoRGB, parseHex, RGBtoHSV } from "../../../util/colorUtils.mjs"; // Types
function has(obj, key) {
return key.every(k => obj.hasOwnProperty(k));
}
export function parseColor(color) {
if (!color) return null;
let hsva = null;
if (typeof color === 'string') {
const hex = parseHex(color);
hsva = HexToHSV(hex);
}
if (typeof color === 'object') {
if (has(color, ['r', 'g', 'b'])) {
hsva = RGBtoHSV(color);
} else if (has(color, ['h', 's', 'l'])) {
hsva = HSLtoHSV(color);
} else if (has(color, ['h', 's', 'v'])) {
hsva = color;
}
}
return hsva;
}
function stripAlpha(color, stripAlpha) {
if (stripAlpha) {
const {
a,
...rest
} = color;
return rest;
}
return color;
}
export function extractColor(color, input) {
if (input == null || typeof input === 'string') {
const hex = HSVtoHex(color);
if (color.a === 1) return hex.slice(0, 7);else return hex;
}
if (typeof input === 'object') {
let converted;
if (has(input, ['r', 'g', 'b'])) converted = HSVtoRGB(color);else if (has(input, ['h', 's', 'l'])) converted = HSVtoHSL(color);else if (has(input, ['h', 's', 'v'])) converted = color;
return stripAlpha(converted, !has(input, ['a']) && color.a === 1);
}
return color;
}
export function hasAlpha(color) {
if (!color) return false;
if (typeof color === 'string') {
return color.length > 7;
}
if (typeof color === 'object') {
return has(color, ['a']) || has(color, ['alpha']);
}
return false;
}
export const nullColor = {
h: 0,
s: 0,
v: 1,
a: 1
};
const rgba = {
inputProps: {
type: 'number',
min: 0
},
inputs: [{
label: 'R',
max: 255,
step: 1,
getValue: c => Math.round(c.r),
getColor: (c, v) => ({
...c,
r: Number(v)
})
}, {
label: 'G',
max: 255,
step: 1,
getValue: c => Math.round(c.g),
getColor: (c, v) => ({
...c,
g: Number(v)
})
}, {
label: 'B',
max: 255,
step: 1,
getValue: c => Math.round(c.b),
getColor: (c, v) => ({
...c,
b: Number(v)
})
}, {
label: 'A',
max: 1,
step: 0.01,
getValue: _ref => {
let {
a
} = _ref;
return a ? Math.round(a * 100) / 100 : 1;
},
getColor: (c, v) => ({
...c,
a: Number(v)
})
}],
to: HSVtoRGB,
from: RGBtoHSV
};
const rgb = {
...rgba,
inputs: rgba.inputs?.slice(0, 3)
};
const hsla = {
inputProps: {
type: 'number',
min: 0
},
inputs: [{
label: 'H',
max: 360,
step: 1,
getValue: c => Math.round(c.h),
getColor: (c, v) => ({
...c,
h: Number(v)
})
}, {
label: 'S',
max: 1,
step: 0.01,
getValue: c => Math.round(c.s * 100) / 100,
getColor: (c, v) => ({
...c,
s: Number(v)
})
}, {
label: 'L',
max: 1,
step: 0.01,
getValue: c => Math.round(c.l * 100) / 100,
getColor: (c, v) => ({
...c,
l: Number(v)
})
}, {
label: 'A',
max: 1,
step: 0.01,
getValue: _ref2 => {
let {
a
} = _ref2;
return a ? Math.round(a * 100) / 100 : 1;
},
getColor: (c, v) => ({
...c,
a: Number(v)
})
}],
to: HSVtoHSL,
from: HSLtoHSV
};
const hsl = {
...hsla,
inputs: hsla.inputs.slice(0, 3)
};
const hexa = {
inputProps: {
type: 'text'
},
inputs: [{
label: 'HEXA',
getValue: c => c,
getColor: (c, v) => v
}],
to: HSVtoHex,
from: HexToHSV
};
const hex = {
...hexa,
inputs: [{
label: 'HEX',
getValue: c => c.slice(0, 7),
getColor: (c, v) => v
}]
};
export const modes = {
rgb,
rgba,
hsl,
hsla,
hex,
hexa
};
//# sourceMappingURL=index.mjs.map