Skip to content

Commit

Permalink
#14 Add new method to color-name-manager.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
azurepolarbear committed May 26, 2024
1 parent e92a8a1 commit 9fc885d
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 33 deletions.
24 changes: 17 additions & 7 deletions docs/release-notes/v0.6.0-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ declare class StringMap<Type> {
```typescript
/**
* A color to be used in a Palette.
*
* @category Palette
*/
declare interface PaletteColor {
Expand Down Expand Up @@ -200,30 +201,39 @@ declare interface Palette {

```typescript
/**
* Manager to select a random {@link ColorSelector} from a {@link !Set}.
* Manager to store and retrieve the names of colors based on their
* hex string value.
*
* @category Color
*/
declare class ColorNameManager {
/**
* Retrieves the name of the color represented by the given {@link colorHex}.
* If the {@link colorHex} string is not well formatted or the nearest color function
* encounters an error, the method will return `undefined`.
* encounters an error, the method will return {@link !undefined}.
*
* @param colorHex - The hex string representation of the color whose
* name is being retrieved (format: `#RRGGBB`).
* @public
* @static
*/
public static getColorName(colorHex: string): string | undefined;

/**
* Does the given {@link hex} string already have a color name match?
* @param hex -
*
* @param hex
*
* @return `true` if the {@link hex} has a direct color name match in the manager,
* `false` if it does not.
* @public
* @static
*/
public static hasMatch(hex: string): boolean;

/**
* Add the given {@link PaletteColor.HEX} and {@link PaletteColor.NAME}
* to the {@link _MATCHED_COLORS} map.
*
* @param color
*/
public static addColor(color: PaletteColor): void;
}
```

Expand Down
63 changes: 40 additions & 23 deletions src/main/color/color-name/color-name-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import colorNames from 'color-name-list/dist/colornames.json';
import nearestColor from 'nearest-color';

import {StringMap} from 'map';
import {PaletteColor} from 'palette';

interface NearestColorMatch {
name: string,
Expand All @@ -27,46 +28,36 @@ interface NearestColorMatch {
distance: number
}

// TODO - After palette colors have been created, add all palette color names to the manager
// TODO - function should accept a list of palette colors to 'load' names from

const _COLORS = colorNames.reduce((o, {name, hex}) => Object.assign(o, {[name]: hex}), {});
const _NEAREST_COLOR = nearestColor.from(_COLORS);

/**
* Manager to store and retrieve the names of colors based on their
* hex string value.
*
* @category Color
*/
class ColorNameManager {
export class ColorNameManager {
/**
* A map of colors whose names have already been retrieved from the
* nearest color method.
* @private
* @static
* @readonly
*/
private static readonly _MATCHED_COLORS: StringMap<string> = new StringMap<string>();

/**
* Retrieves the name of the color represented by the given {@link colorHex}.
* If the {@link colorHex} string is not well formatted or the nearest color function
* encounters an error, the method will return {@link !undefined}.
*
* @param colorHex - The hex string representation of the color whose
* name is being retrieved (format: `#RRGGBB`).
* @public
* @static
*/
public static getColorName(colorHex: string): string | undefined {
if (!colorHex.startsWith('#')) {
colorHex = '#' + colorHex;
}

colorHex = colorHex.toUpperCase();
colorHex = ColorNameManager.formatHex(colorHex);
let match: string | undefined = undefined;

if (this.hasMatch(colorHex)) {
match = this._MATCHED_COLORS.get(colorHex);
if (ColorNameManager.hasMatch(colorHex)) {
match = ColorNameManager._MATCHED_COLORS.get(colorHex);
} else {
try {
const result: NearestColorMatch | null = _NEAREST_COLOR(colorHex);
Expand All @@ -76,7 +67,7 @@ class ColorNameManager {
}

if (match) {
this._MATCHED_COLORS.setUndefinedKey(colorHex, match);
ColorNameManager._MATCHED_COLORS.setUndefinedKey(colorHex, match);
}
} catch {
match = undefined;
Expand All @@ -92,15 +83,41 @@ class ColorNameManager {

/**
* Does the given {@link hex} string already have a color name match?
* @param hex -
*
* @param hex
*
* @return `true` if the {@link hex} has a direct color name match in the manager,
* `false` if it does not.
* @public
* @static
*/
public static hasMatch(hex: string): boolean {
return this._MATCHED_COLORS.hasKey(hex);
return ColorNameManager._MATCHED_COLORS.hasKey(hex);
}
}

export {ColorNameManager};
/**
* Add the given {@link PaletteColor.HEX} and {@link PaletteColor.NAME}
* to the {@link _MATCHED_COLORS} map.
*
* @param color
*/
public static addColor(color: PaletteColor): void {
const hex: string = ColorNameManager.formatHex(color.HEX);
ColorNameManager._MATCHED_COLORS.setKey(hex, color.NAME);
}

/**
* Adds a hash symbol (#) to the beginning of the given string
* if one is missing and returns the result with all uppercase
* characters.
*
* @param original
*/
private static formatHex(original: string): string {
let hex: string = original;

if (!hex.startsWith('#')) {
hex = '#' + hex;
}

return hex.toUpperCase();
}
}
4 changes: 1 addition & 3 deletions src/main/color/palette/palette-color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@

// color names derived from https://colornamer.robertcooper.me/

import {Palette} from './palette';

/**
* A color to be used in a {@link Palette}.
* A color to be used in a Palette.
*
* @category Palette
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import {BLACK_PALETTE_COLORS} from 'palette-colors';
import {BLACK_HEXES, checkForValidStringMap} from 'unit-test/shared';

// TODO - test that hex in all palette colors is properly formatted (REGEX!)

describe('black palette colors', (): void => {
test('valid string map: BLACK_PALETTE_COLORS', (): void => {
checkForValidStringMap(BLACK_PALETTE_COLORS);
Expand Down

0 comments on commit 9fc885d

Please sign in to comment.