Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
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