Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions projects/common/src/color/color-palette.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,26 @@ describe('Color palette', () => {
expect(() => new ColorPalette(['black'])).toThrow();
expect(() => new ColorPalette(['white', 'black'])).not.toThrow();
});

test('should return color combinations correctly', () => {
const palette = new ColorPalette(['#fffbeb', '#140300']);
expect(palette.getColorCombinations(2)).toEqual([
{
background: 'rgb(255, 251, 235)',
foreground: '#080909'
},
{
background: 'rgb(20, 3, 0)',
foreground: '#FFFFFF'
}
]);
});

test('should generate color for a string as expected from a limited set', () => {
const palette = new ColorPalette(['#fffbeb', '#140300', '#789ab7']);
expect(palette.getColorCombinationForId(2, 'test')).toEqual({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at how this is used, we could probably make the count optional and the second param. so just getColorCombinationForString('test')

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense. updated @aaron-steinfeld

background: 'rgb(255, 251, 235)',
foreground: '#080909'
});
});
});
21 changes: 21 additions & 0 deletions projects/common/src/color/color-palette.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { rgb } from 'd3-color';
import { interpolateRgbBasis, quantize } from 'd3-interpolate';
import { hashCode } from '../utilities/math/math-utilities';
import { Color, ColorCombination } from './color';

export class ColorPalette {
private readonly basisColors: string[];
Expand All @@ -10,6 +12,25 @@ export class ColorPalette {
this.basisColors = basisColors.map(color => rgb(color).toString());
}

public getColorCombinations(count: number): ColorCombination[] {
return this.forNColors(count).map(color => ({ background: color, foreground: this.getContrast(color) }));
}

public getColorCombinationForId(colorCount: number, id: string): ColorCombination {
return this.getColorCombinations(colorCount)[Math.abs(hashCode(id)) % colorCount];
}

private getContrast(rgbColorString: string): string {
// Convert to RGB value
const rgbColor = rgb(rgbColorString);

// Get YIQ ratio
const yiq = (rgbColor.r * 299 + rgbColor.g * 587 + rgbColor.b * 114) / 1000;

// Check contrast
return yiq >= 128 ? Color.Gray9 : Color.White;
}

public forNColors(count: number): string[] {
if (count === this.basisColors.length) {
// Use as is if palette size matches, don't interpolate
Expand Down
19 changes: 19 additions & 0 deletions projects/common/src/color/color.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { hashCode } from '../utilities/math/math-utilities';

export const enum Color {
Blue1 = '#f0f6ff',
Blue2 = '#b8d3ff',
Expand Down Expand Up @@ -58,3 +60,20 @@ export const enum Color {
Yellow8 = '#6d5b00',
Yellow9 = '#181400'
}

export interface ColorCombination {
background: string;
foreground: string;
}

export const getHexColorForString = (id: string): string => {
const hash = hashCode(id);
let rgb = '#';
for (let i = 0; i < 3; i++) {
// tslint:disable-next-line: no-bitwise
const value = (hash >> (i * 8)) & 0xff;
rgb += `00${value.toString(16)}`.substr(-2);
}

return rgb;
};
3 changes: 3 additions & 0 deletions projects/common/src/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,6 @@ export * from './time/page-time-range-preference.service';

// Validators
export * from './utilities/validators';

// Color Palette
export * from './color/color-palette';
5 changes: 5 additions & 0 deletions projects/common/src/utilities/math/math-utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ export const getPercentage = (numerator: number | undefined, denominator: number
return (numerator / denominator) * 100;
};

// Trying to recreate Java hashcode
export const hashCode = (str: string): number =>
// tslint:disable-next-line: no-bitwise
Array.from(str).reduce((s, c) => (Math.imul(31, s) + c.charCodeAt(0)) | 0, 0);

export interface Point {
x: number;
y: number;
Expand Down