Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 6 additions & 1 deletion projects/common/src/color/color-palette.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,14 @@ describe('Color palette', () => {

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({
expect(palette.getColorCombinationForId('test', 2)).toEqual({
background: 'rgb(255, 251, 235)',
foreground: '#080909'
});
expect(palette.getColorCombinationForId('test')).toEqual({
background: 'rgb(20, 3, 0)',
foreground: '#FFFFFF'
});

});
});
6 changes: 4 additions & 2 deletions projects/common/src/color/color-palette.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ export class ColorPalette {
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];
public getColorCombinationForId(id: string, colorPaletteSize?: number): ColorCombination {
const colorSetSize = colorPaletteSize ?? this.basisColors.length;

return this.getColorCombinations(colorSetSize)[Math.abs(hashCode(id)) % colorSetSize];
}

private getContrast(rgbColorString: string): string {
Expand Down