-
-
Save ariel-frischer/8e83dd872c30dfb539552866661cf1c5 to your computer and use it in GitHub Desktop.
TypeScript Color class, useful to manipulate rgba components and convert to rga/rgba/hex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const RGB_COLOR_REGEX = /\((\d+),\s*(\d+),\s*(\d+)(,\s*(\d*.\d*))?\)/; | |
export class Color { | |
public r: number; | |
public g: number; | |
public b: number; | |
public a: number; | |
constructor() | |
constructor(colorStr?: string) | |
constructor(r?: string|number, g?: number, b?: number) | |
constructor(r?: string|number, g?: number, b?: number, a?: number) { | |
if (typeof r === 'string') { | |
r = r.trim(); | |
if (r.indexOf('#') === 0) { | |
r = r.substr(r.indexOf('#') + 1); | |
this.r = parseInt(r.substr(0, 2), 16); | |
this.g = parseInt(r.substr(2, 2), 16); | |
this.b = parseInt(r.substr(4, 2), 16); | |
} else if (r.indexOf('rgb') === 0) { | |
const res = RGB_COLOR_REGEX.exec(r); | |
this.r = parseInt(res[1], 10); | |
this.g = parseInt(res[2], 10); | |
this.b = parseInt(res[3], 10); | |
this.a = res[5] ? parseFloat(res[5]) : 1; | |
} | |
} else { | |
this.r = r; | |
this.g = g; | |
this.b = b; | |
this.a = a || 1; | |
} | |
} | |
toHex() { | |
return '#' + this.r.toString(16) + this.g.toString(16) + this.b.toString(16); | |
} | |
toRgb() { | |
return `rgb(${this.r}, ${this.g}, ${this.b})`; | |
} | |
toRgba() { | |
return `rgba(${this.r}, ${this.g}, ${this.b}, ${this.a})`; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment