Skip to content

Commit d387a83

Browse files
committed
Improvement - Config - Add support for shorthand hex colors
1 parent 1762295 commit d387a83

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

TestingArena/ArenaVueUiXy.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,8 @@ const model = ref([
376376
{ key: 'useCanvas', def: false, type: 'checkbox' }, // DEPRECATED (removed)
377377
{ key: 'useCssAnimation', def: true, type: 'checkbox', label: 'useCssAnimation', category: 'general' },
378378
{ key: 'chart.fontFamily', def: 'inherit', type: 'text', label: 'fontFamily', category: 'general' },
379-
{ key: 'chart.backgroundColor', def: 'transparent', type: 'color', label: 'backgroundColor', category: 'general' },
380-
{ key: 'chart.color', def: '#FFFFFF', type: 'color', label: 'textColor', category: 'general' },
379+
{ key: 'chart.backgroundColor', def: '#FFF', type: 'color', label: 'backgroundColor', category: 'general' },
380+
{ key: 'chart.color', def: '#111', type: 'color', label: 'textColor', category: 'general' },
381381
{ key: 'chart.height', def: 600, type: 'range', min: 300, max: 1000, label: 'height', category: 'general' },
382382
{ key: 'chart.width', def: 1000, type: 'range', min: 300, max: 2000, label: 'width', category: 'general' },
383383
{ key: 'chart.zoom.show', def: true, type: 'checkbox', label: 'zoom', category: 'general' },

src/lib.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -393,19 +393,25 @@ export const opacity = ["00", "03", "05", "08", "0A", "0D", "0F", "12", "14", "1
393393

394394
export function convertColorToHex(color) {
395395
const hexRegex = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})?$/i;
396+
const shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])([a-f\d])?$/i;
396397
const rgbRegex = /^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*([\d.]+))?\)$/i;
397398
const hslRegex = /^hsla?\((\d+),\s*([\d.]+)%,\s*([\d.]+)%(?:,\s*([\d.]+))?\)$/i;
398399

399-
if ([undefined, null, NaN].includes(color)) {
400+
if (color === undefined || color === null || (typeof color === 'number' && isNaN(color))) {
400401
return null;
401402
}
402403

403404
color = convertNameColorToHex(color);
404405

406+
405407
if (color === 'transparent') {
406408
return "#FFFFFF00";
407409
}
408410

411+
color = color.replace(shorthandRegex, (_, r, g, b, a) => {
412+
return `#${r}${r}${g}${g}${b}${b}${a ? a + a : ''}`;
413+
});
414+
409415
let match;
410416
let alpha = 1;
411417

@@ -420,8 +426,8 @@ export function convertColorToHex(color) {
420426
} else if ((match = color.match(hslRegex))) {
421427
const [, h, s, l, a] = match;
422428
alpha = a ? parseFloat(a) : 1;
423-
const rgb = hslToRgba(Number(h), Number(s), Number(l));
424-
return `#${decimalToHex(rgb[0])}${decimalToHex(rgb[1])}${decimalToHex(rgb[2])}${decimalToHex(Math.round(alpha * 255))}`;
429+
const [rr, gg, bb] = hslToRgba(Number(h), Number(s), Number(l));
430+
return `#${decimalToHex(rr)}${decimalToHex(gg)}${decimalToHex(bb)}${decimalToHex(Math.round(alpha * 255))}`;
425431
}
426432

427433
return null;

tests/lib.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,23 @@ describe("convertColorToHex", () => {
399399
const result = convertColorToHex("hsla(0, 100%, 50%, 0.5)");
400400
expect(result).toBe("#ff000080");
401401
});
402+
403+
test("returns HEX color format from shorthand hex codes (#RGB, #RGBA)", () => {
404+
expect(convertColorToHex("#ABC")).toBe("#AABBCCff");
405+
expect(convertColorToHex("ABC")).toBe("#AABBCCff");
406+
expect(convertColorToHex("#abc")).toBe("#aabbccff");
407+
expect(convertColorToHex("abc")).toBe("#aabbccff");
408+
expect(convertColorToHex("#ABC8")).toBe("#AABBCC88");
409+
expect(convertColorToHex("ABC8")).toBe("#AABBCC88");
410+
expect(convertColorToHex("#abc8")).toBe("#aabbcc88");
411+
expect(convertColorToHex("abc8")).toBe("#aabbcc88");
412+
});
413+
414+
test("returns null for invalid or empty input", () => {
415+
expect(convertColorToHex(null)).toBeNull();
416+
expect(convertColorToHex(undefined)).toBeNull();
417+
expect(convertColorToHex("not-a-color")).toBeNull();
418+
});
402419
});
403420

404421
describe("shiftHue", () => {

0 commit comments

Comments
 (0)