Helpers: fix color conversion functions and add tests.

master
Tom Hacohen 5 years ago
parent 7fbac2c401
commit a4c8fd6688

@ -0,0 +1,22 @@
import { colorHtmlToInt, colorIntToHtml } from './journal-processors';
it('Color conversion', () => {
const testColors = [
'#aaaaaaaa',
'#00aaaaaa',
'#0000aaaa',
'#000000aa',
'#00000000',
'#bb00bbbb',
'#bb0000bb',
'#bb000000',
'#11110011',
'#11110000',
'#11111100',
];
for (const color of testColors) {
expect(color).toEqual(colorIntToHtml(colorHtmlToInt(color)));
}
});

@ -36,9 +36,11 @@ export function syncEntriesToItemMap(
return items; return items;
} }
function colorIntToHtml(color: number) { export const defaultColor = '#8BC34A';
export function colorIntToHtml(color?: number) {
if (color === undefined) { if (color === undefined) {
return '#8BC34A'; return defaultColor;
} }
// tslint:disable:no-bitwise // tslint:disable:no-bitwise
@ -53,8 +55,26 @@ function colorIntToHtml(color: number) {
return (ret.length === 1) ? '0' + ret : ret; return (ret.length === 1) ? '0' + ret : ret;
} }
return '#' + toHex(red) + toHex(green) + toHex(blue) + return '#' + toHex(red) + toHex(green) + toHex(blue) + toHex(alpha);
((alpha > 0) ? toHex(alpha) : ''); }
export function colorHtmlToInt(color?: string) {
if (!color) {
color = defaultColor;
}
const match = color.match(/^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})?$/i);
if (!match) {
return undefined;
}
const r = parseInt(match[1], 16);
const g = parseInt(match[2], 16);
const b = parseInt(match[3], 16);
const a = (match[4]) ? parseInt(match[4], 16) : 0xFF;
return (b | (g << 8) | (r << 16) | (a << 24));
} }
function syncEntriesToCalendarItemMap<T extends EventType>( function syncEntriesToCalendarItemMap<T extends EventType>(

Loading…
Cancel
Save