Skip to content

Commit

Permalink
Updated hexadecimal string parsing in RGBColor.fromHex method.
Browse files Browse the repository at this point in the history
  • Loading branch information
armcburney committed Jun 11, 2018
1 parent 7fabea2 commit e2605b2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
15 changes: 10 additions & 5 deletions src/colors/RGBColor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,21 @@ export class RGBColor implements Color {
*
* @class RGBColor
* @method fromHex
* @param {string} value A hex string of the form `FF0000`.
* @param {string} value A hex string of the form `FF0000` or `#FF0000`.
* @returns {RGBColor}
*/
public static fromHex(value: string): RGBColor {
if (value.length !== 6) {
throw new Error('Please pass in a hexadecimal string (i.e., FF33AA)');
// Ensure the user passed in a valid hexadecimal string
if (
value.length > 7 ||
value.length < 6 ||
(value.length === 7 && value.lastIndexOf('#', 0) !== 0)
) {
throw new Error('Please pass in a valid hexadecimal string (i.e., FF33AA or #FF33AA)');
}

// Split the hexadecimal string into segments of length 2
const matches = value.match(/.{2}/g);
const matches = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(value);

// Throw error if `matches` is null. Shouldn't be the case because of
// the previous assertion - but tslint was complaining ¯\_(ツ)_/¯
Expand All @@ -62,7 +67,7 @@ export class RGBColor implements Color {
}

// Map the hexadecimal string segments to decimal integers
const hexValues = matches.map((hex: string) => parseInt(hex, 16));
const hexValues = matches.slice(1).map((hex: string) => parseInt(hex, 16));

// Return the new color
return new RGBColor(hexValues[0], hexValues[1], hexValues[2]);
Expand Down
10 changes: 4 additions & 6 deletions tests/colors/RGBColor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,24 @@ describe('RGBColor', () => {
describe('RGBColor.fromHex', () => {
it('properly returns the appropriate normalized RGB values', () => {
const red: RGBColor = RGBColor.fromHex('FF0000');
const redTwo: RGBColor = RGBColor.fromHex('#FF0000');
const green: RGBColor = RGBColor.fromHex('00FF00');
const blue: RGBColor = RGBColor.fromHex('0000FF');

expect(red.asArray()).toEqual([1, 0, 0]);
expect(redTwo.asArray()).toEqual([1, 0, 0]);
expect(green.asArray()).toEqual([0, 1, 0]);
expect(blue.asArray()).toEqual([0, 0, 1]);
});

it('properly throws errors for invalid hexadecimal strings', () => {
expect(() => {
RGBColor.fromHex('#FF0000');
}).toThrow('Please pass in a hexadecimal string (i.e., FF33AA)');

expect(() => {
RGBColor.fromHex('FF');
}).toThrow('Please pass in a hexadecimal string (i.e., FF33AA)');
}).toThrow('Please pass in a valid hexadecimal string (i.e., FF33AA or #FF33AA)');

expect(() => {
RGBColor.fromHex('FFFFFFF');
}).toThrow('Please pass in a hexadecimal string (i.e., FF33AA)');
}).toThrow('Please pass in a valid hexadecimal string (i.e., FF33AA or #FF33AA)');
});
});

Expand Down

0 comments on commit e2605b2

Please sign in to comment.