|
|
|
@ -26,42 +26,35 @@ const ColorDefinitions: IColorDefinition[] = [ |
|
|
|
{ |
|
|
|
regex: /^rgba\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3}),\s*([\d\.]{1,})\)$/, |
|
|
|
process: (bits) => { |
|
|
|
return { |
|
|
|
r: parseInt(bits[1], 10) / 255, |
|
|
|
g: parseInt(bits[2], 10) / 255, |
|
|
|
b: parseInt(bits[3], 10) / 255, |
|
|
|
a: parseFloat(bits[4]) |
|
|
|
}; |
|
|
|
return createColor( |
|
|
|
parseInt(bits[1], 10) / 255, |
|
|
|
parseInt(bits[2], 10) / 255, |
|
|
|
parseInt(bits[3], 10) / 255, |
|
|
|
parseFloat(bits[4])); |
|
|
|
} |
|
|
|
}, { |
|
|
|
regex: /^rgb\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})\)$/, |
|
|
|
process: (bits) => { |
|
|
|
return { |
|
|
|
r: parseInt(bits[1], 10) / 255, |
|
|
|
g: parseInt(bits[2], 10) / 255, |
|
|
|
b: parseInt(bits[3], 10) / 255, |
|
|
|
a: 1 |
|
|
|
}; |
|
|
|
return createColor( |
|
|
|
parseInt(bits[1], 10) / 255, |
|
|
|
parseInt(bits[2], 10) / 255, |
|
|
|
parseInt(bits[3], 10) / 255); |
|
|
|
} |
|
|
|
}, { |
|
|
|
regex: /^(\w{2})(\w{2})(\w{2})$/, |
|
|
|
process: (bits) => { |
|
|
|
return { |
|
|
|
r: parseInt(bits[1], 16) / 255, |
|
|
|
g: parseInt(bits[2], 16) / 255, |
|
|
|
b: parseInt(bits[3], 16) / 255, |
|
|
|
a: 1 |
|
|
|
}; |
|
|
|
return createColor( |
|
|
|
parseInt(bits[1], 16) / 255, |
|
|
|
parseInt(bits[2], 16) / 255, |
|
|
|
parseInt(bits[3], 16) / 255); |
|
|
|
} |
|
|
|
}, { |
|
|
|
regex: /^(\w{1})(\w{1})(\w{1})$/, |
|
|
|
process: (bits) => { |
|
|
|
return { |
|
|
|
r: parseInt(bits[1] + bits[1], 16) / 255, |
|
|
|
g: parseInt(bits[2] + bits[2], 16) / 255, |
|
|
|
b: parseInt(bits[3] + bits[3], 16) / 255, |
|
|
|
a: 1 |
|
|
|
}; |
|
|
|
return createColor( |
|
|
|
parseInt(bits[1] + bits[1], 16) / 255, |
|
|
|
parseInt(bits[2] + bits[2], 16) / 255, |
|
|
|
parseInt(bits[3] + bits[3], 16) / 255); |
|
|
|
} |
|
|
|
} |
|
|
|
]; |
|
|
|
@ -165,6 +158,53 @@ export module MathHelper { |
|
|
|
return undefined; |
|
|
|
} |
|
|
|
|
|
|
|
export function randomColor() { |
|
|
|
return colorToString(colorFromHsv(Math.random() * 360, .9, .9)); |
|
|
|
} |
|
|
|
|
|
|
|
export function colorToString(color: Color): string { |
|
|
|
let r = Math.round(color.r * 255).toString(16); |
|
|
|
let g = Math.round(color.g * 255).toString(16); |
|
|
|
let b = Math.round(color.b * 255).toString(16); |
|
|
|
|
|
|
|
if (r.length === 1) { |
|
|
|
r = '0' + r; |
|
|
|
} |
|
|
|
if (g.length === 1) { |
|
|
|
g = '0' + g; |
|
|
|
} |
|
|
|
if (b.length === 1) { |
|
|
|
b = '0' + b; |
|
|
|
} |
|
|
|
|
|
|
|
return '#' + r + g + b; |
|
|
|
} |
|
|
|
|
|
|
|
export function colorFromHsv(h: number, s: number, v: number): Color { |
|
|
|
const hi = Math.floor(h / 60) % 6; |
|
|
|
|
|
|
|
const f = (h / 60) - Math.floor(h / 60); |
|
|
|
|
|
|
|
const p = (v * (1 - s)); |
|
|
|
const q = (v * (1 - (f * s))); |
|
|
|
const t = (v * (1 - ((1 - f) * s))); |
|
|
|
|
|
|
|
switch (hi) { |
|
|
|
case 0: |
|
|
|
return createColor(v, t, p); |
|
|
|
case 1: |
|
|
|
return createColor(q, v, p); |
|
|
|
case 2: |
|
|
|
return createColor(p, v, t); |
|
|
|
case 3: |
|
|
|
return createColor(p, q, v); |
|
|
|
case 4: |
|
|
|
return createColor(t, p, v); |
|
|
|
default: |
|
|
|
return createColor(v, p, q); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
export function toLuminance(color: Color) { |
|
|
|
if (!color) { |
|
|
|
return 1; |
|
|
|
@ -172,4 +212,8 @@ export module MathHelper { |
|
|
|
|
|
|
|
return (0.2126 * color.r + 0.7152 * color.g + 0.0722 * color.b) / color.a; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
export function createColor(r: number, g: number, b: number, a = 1) { |
|
|
|
return { r, g, b, a }; |
|
|
|
} |