diff --git a/docs/release-notes/v0.6.0-notes.md b/docs/release-notes/v0.6.0-notes.md index be4678e..1c9d664 100644 --- a/docs/release-notes/v0.6.0-notes.md +++ b/docs/release-notes/v0.6.0-notes.md @@ -107,6 +107,7 @@ declare class StringMap { ```typescript /** * A color to be used in a Palette. + * * @category Palette */ declare interface PaletteColor { @@ -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; } ``` diff --git a/src/main/color/color-name/color-name-manager.ts b/src/main/color/color-name/color-name-manager.ts index 57ee84f..524af3a 100644 --- a/src/main/color/color-name/color-name-manager.ts +++ b/src/main/color/color-name/color-name-manager.ts @@ -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, @@ -27,24 +28,19 @@ 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 = new StringMap(); @@ -52,21 +48,16 @@ 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 {@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); @@ -76,7 +67,7 @@ class ColorNameManager { } if (match) { - this._MATCHED_COLORS.setUndefinedKey(colorHex, match); + ColorNameManager._MATCHED_COLORS.setUndefinedKey(colorHex, match); } } catch { match = undefined; @@ -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(); + } +} diff --git a/src/main/color/palette/palette-color.ts b/src/main/color/palette/palette-color.ts index 99f2a28..05dfa2d 100644 --- a/src/main/color/palette/palette-color.ts +++ b/src/main/color/palette/palette-color.ts @@ -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 */ diff --git a/src/test/color/palette/palette-colors/black/black-palette-colors.test.ts b/src/test/color/palette/palette-colors/black/black-palette-colors.test.ts index 8fac064..cd9ac89 100644 --- a/src/test/color/palette/palette-colors/black/black-palette-colors.test.ts +++ b/src/test/color/palette/palette-colors/black/black-palette-colors.test.ts @@ -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);