vuetify
Version:
Vue Material Component Framework
196 lines • 3.78 kB
JavaScript
// Utilities
import { HexToHSV, HSLtoHSV, HSVtoHex, HSVtoHSL, HSVtoRGB, RGBtoHSV } from "../../../util/colorUtils.js";
import { has } from "../../../util/helpers.js"; // Types
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 hasA = color.a !== 1;
if (input?.startsWith('rgb(')) {
const {
r,
g,
b,
a
} = HSVtoRGB(color);
return `rgb(${r} ${g} ${b}` + (hasA ? ` / ${a})` : ')');
} else if (input?.startsWith('hsl(')) {
const {
h,
s,
l,
a
} = HSVtoHSL(color);
return `hsl(${h} ${Math.round(s * 100)} ${Math.round(l * 100)}` + (hasA ? ` / ${a})` : ')');
}
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: 0,
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 != null ? 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 != null ? 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.js.map