Skip to content

Commit

Permalink
#14 #88 Complete Discriminator implementation and unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
azurepolarbear committed Jun 1, 2024
1 parent 6f8555f commit f29aab3
Show file tree
Hide file tree
Showing 17 changed files with 195 additions and 25 deletions.
1 change: 1 addition & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const config: JestConfigWithTsJest = {
moduleNameMapper: {
'^color$': '<rootDir>/src/main/color',
'^context$': '<rootDir>/src/main/p5',
'^discriminator$': '<rootDir>/src/main/discriminator',
'^map$': '<rootDir>/src/main/map',
'^math$': '<rootDir>/src/main/math',
'^palette$': '<rootDir>/src/main/color/palette',
Expand Down
11 changes: 6 additions & 5 deletions src/main/batpb-genart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
* See the GNU Affero General Public License for more details.
*/

export * from 'math';
export * from 'random';
export * from 'map';
export * from 'context';
export * from 'color'
export * from 'context';
export * from 'discriminator';
export * from 'map';
export * from 'math';
export * from 'palette';
export * from 'palette-colors';
export * from 'palettes';
export * from 'palette-colors';
export * from 'random';
5 changes: 3 additions & 2 deletions src/main/color/color-contrast/color-contrast-assessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
import {getContrastRatios, ResponseObject} from 'cococh';

import {Color} from 'color';
import {isPaletteColor, PaletteColor} from 'palette';
import {Discriminator} from 'discriminator';
import {PaletteColor} from 'palette';

//TODO - Documentation
// TODO - unit tests
Expand Down Expand Up @@ -64,7 +65,7 @@ export class ColorContrastAssessor {
if (colorA instanceof Color && colorB instanceof Color) {
hexA = colorA.hex;
hexB = colorB.hex;
} else if (isPaletteColor(colorA) && isPaletteColor(colorB)) {
} else if (Discriminator.isPaletteColor(colorA) && Discriminator.isPaletteColor(colorB)) {
hexA = colorA.HEX;
hexB = colorB.HEX;
} else if (typeof colorA === 'string' && typeof colorB === 'string') {
Expand Down
2 changes: 1 addition & 1 deletion src/main/color/color-selector-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*
* @category Color
*/
export const enum ColorSelectorType {
export enum ColorSelectorType {
/**
* Color selector that chooses colors from a pre-selected palette.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/main/color/default-color-selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class DefaultColorSelector extends ColorSelector {
}

/**
* @returns {@link ColorSelectorType.Default}
* @returns {@link ColorSelectorType.DEFAULT}
* @public
*/
public get type(): ColorSelectorType {
Expand Down
12 changes: 3 additions & 9 deletions src/main/color/palette/palette-color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@

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

// TODO - update release notes
import {Discriminators} from 'discriminator';

/**
* A color to be used in a {@link Palette}.
*
Expand Down Expand Up @@ -77,12 +78,5 @@ export interface PaletteColor {
/**
* Discriminator for determining if an object is a PaletteColor.
*/
readonly DISCRIMINATOR: 'I_PALETTE-COLOR';
}

//TODO - add documentation
// TODO - add unit tests
// TODO - add to release notes
export function isPaletteColor(object: any): object is PaletteColor {
return object.DISCRIMINATOR === 'I_PALETTE-COLOR';
readonly DISCRIMINATOR: Discriminators.PALETTE_COLOR;
}
4 changes: 3 additions & 1 deletion src/main/color/palette/palette-colors/black/121212.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

import {ColorNameManager} from 'color';
import {Discriminators} from 'discriminator';
import {PaletteColor} from 'palette';

import {ALL_PALETTE_COLORS, BLACK_PALETTE_COLORS} from '../palette-color-maps';
Expand All @@ -38,7 +39,8 @@ export const _121212: PaletteColor = {
HEX: '#121212',
RGB: {R: 18, G: 18, B: 18},
HSL: {H: 0, S: 0, L: 7},
NAME: 'dark tone ink'
NAME: 'dark tone ink',
DISCRIMINATOR: Discriminators.PALETTE_COLOR
};

BLACK_PALETTE_COLORS.setUndefinedKey(_121212.HEX, _121212);
Expand Down
4 changes: 3 additions & 1 deletion src/main/color/palette/palette-colors/blue/0437f2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

import {ColorNameManager} from 'color';
import {Discriminators} from 'discriminator';
import {PaletteColor} from 'palette';

import {ALL_PALETTE_COLORS, BLUE_PALETTE_COLORS} from '../palette-color-maps';
Expand All @@ -38,7 +39,8 @@ export const _0437F2: PaletteColor = {
HEX: '#0437F2',
RGB: {R: 4, G: 55, B: 241},
HSL: {H: 227, S: 97, L: 48},
NAME: 'vibrant blue'
NAME: 'vibrant blue',
DISCRIMINATOR: Discriminators.PALETTE_COLOR
};

BLUE_PALETTE_COLORS.setUndefinedKey(_0437F2.HEX, _0437F2);
Expand Down
4 changes: 3 additions & 1 deletion src/main/color/palette/palette-colors/green/0fff4f.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

import {ColorNameManager} from 'color';
import {Discriminators} from 'discriminator';
import {PaletteColor} from 'palette';

import {ALL_PALETTE_COLORS, GREEN_PALETTE_COLORS} from '../palette-color-maps';
Expand All @@ -38,7 +39,8 @@ export const _0FFF4F: PaletteColor = {
HEX: '#0FFF4F',
RGB: {R: 15, G: 255, B: 79},
HSL: {H: 136, S: 100, L: 53},
NAME: 'cathode green'
NAME: 'cathode green',
DISCRIMINATOR: Discriminators.PALETTE_COLOR
};

GREEN_PALETTE_COLORS.setUndefinedKey(_0FFF4F.HEX, _0FFF4F);
Expand Down
4 changes: 3 additions & 1 deletion src/main/color/palette/palette-colors/pink/ff6bb5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

import {ColorNameManager} from 'color';
import {Discriminators} from 'discriminator';
import {PaletteColor} from 'palette';

import {ALL_PALETTE_COLORS, PINK_PALETTE_COLORS} from '../palette-color-maps';
Expand All @@ -38,7 +39,8 @@ export const _FF6BB5: PaletteColor = {
HEX: '#FF6BB5',
RGB: {R: 255, G: 107, B: 181},
HSL: {H: 330, S: 100, L: 71},
NAME: 'fen hong pink'
NAME: 'fen hong pink',
DISCRIMINATOR: Discriminators.PALETTE_COLOR
};

PINK_PALETTE_COLORS.setUndefinedKey(_FF6BB5.HEX, _FF6BB5);
Expand Down
4 changes: 3 additions & 1 deletion src/main/color/palette/palette-colors/purple/7a00f5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

import {ColorNameManager} from 'color';
import {Discriminators} from 'discriminator';
import {PaletteColor} from 'palette';

import {ALL_PALETTE_COLORS, PURPLE_PALETTE_COLORS} from '../palette-color-maps';
Expand All @@ -38,7 +39,8 @@ export const _7A00F5: PaletteColor = {
HEX: '#7A00F5',
RGB: {R: 122, G: 0, B: 245},
HSL: {H: 270, S: 100, L: 48},
NAME: 'poison purple'
NAME: 'poison purple',
DISCRIMINATOR: Discriminators.PALETTE_COLOR
};

PURPLE_PALETTE_COLORS.setUndefinedKey(_7A00F5.HEX, _7A00F5);
Expand Down
39 changes: 39 additions & 0 deletions src/main/discriminator/discriminators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (C) 2024 brittni and the polar bear LLC.
*
* This file is a part of brittni and the polar bear's Generative Art Library,
* which is released under the GNU Affero General Public License, Version 3.0.
* You may not use this file except in compliance with the license.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. See LICENSE or go to
* https://www.gnu.org/licenses/agpl-3.0.en.html for full license details.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
*/

/**
* The collection of discriminator values for library interfaces.
* All values should be unique.
*
* @category Discriminator
*/
export enum Discriminators {
/**
* Discriminator for the {@link Palette} interface.
*/
PALETTE = 'I_PALETTE',

/**
* Discriminator for the {@link PaletteColor} interface.
*/
PALETTE_COLOR = 'I_PALETTE-COLOR',

/**
* Discriminator for the {@link WeightedElement} interface.
*/
WEIGHTED_ELEMENT = 'I_WEIGHTED-ELEMENT'
}
19 changes: 19 additions & 0 deletions src/main/discriminator/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright (C) 2024 brittni and the polar bear LLC.
*
* This file is a part of brittni and the polar bear's Generative Art Library,
* which is released under the GNU Affero General Public License, Version 3.0.
* You may not use this file except in compliance with the license.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. See LICENSE or go to
* https://www.gnu.org/licenses/agpl-3.0.en.html for full license details.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
*/

export * from './discriminator';
export * from './discriminators';
6 changes: 4 additions & 2 deletions src/test/color/color-name/color-name-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
import nearestColor from 'nearest-color';

import {ColorNameManager} from 'color';
import {Discriminators} from 'discriminator';
import {StringMap} from 'map';
import {PaletteColor} from "palette";
import {PaletteColor} from 'palette';

describe('color name manager test', (): void => {
let nearestColorSpy: any;
Expand Down Expand Up @@ -78,7 +79,8 @@ describe('color name manager test', (): void => {
RGB: {R: 0, G: 0, B: 0},
HSL: {H: 0, S: 0, L: 0},
HEX: '#000000',
NAME: 'test fake color'
NAME: 'test fake color',
DISCRIMINATOR: Discriminators.PALETTE_COLOR
};

const hex: string = fakeColor.HEX;
Expand Down
77 changes: 77 additions & 0 deletions src/test/discriminator/discriminator.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright (C) 2024 brittni and the polar bear LLC.
*
* This file is a part of brittni and the polar bear's Generative Art Library,
* which is released under the GNU Affero General Public License, Version 3.0.
* You may not use this file except in compliance with the license.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. See LICENSE or go to
* https://www.gnu.org/licenses/agpl-3.0.en.html for full license details.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
*/

import {Discriminator, Discriminators} from 'discriminator';
import {PaletteColor} from 'palette';
import {_0FFF4F} from 'palette-colors';

describe('discriminator tests', (): void => {
describe('discriminator tests: isPaletteColor', (): void => {
test('PaletteColor discriminator method: null', (): void => {
expect(Discriminator.isPaletteColor(null)).toBeFalsy();
});

test('PaletteColor discriminator method: undefined', (): void => {
expect(Discriminator.isPaletteColor(undefined)).toBeFalsy();
});

test('PaletteColor discriminator method: number', (): void => {
expect(Discriminator.isPaletteColor(10)).toBeFalsy();
});

test('PaletteColor discriminator method: string', (): void => {
expect(Discriminator.isPaletteColor('my test string')).toBeFalsy();
});

test('PaletteColor discriminator method: boolean', (): void => {
expect(Discriminator.isPaletteColor(true)).toBeFalsy();
});

test('PaletteColor discriminator method: other object', (): void => {
const myObject: {KEY: string} = {
KEY: 'the key'
}

expect(Discriminator.isPaletteColor(myObject)).toBeFalsy();
});

test('PaletteColor discriminator method: other object with discriminator', (): void => {
const myObject: {KEY: string, DISCRIMINATOR: 'other'} = {
KEY: 'the key',
DISCRIMINATOR: 'other'
}

expect(Discriminator.isPaletteColor(myObject)).toBeFalsy();
});

test('PaletteColor discriminator method: PaletteColor', (): void => {
expect(Discriminator.isPaletteColor(_0FFF4F)).toBeTruthy();
});

test('PaletteColor discriminator method: Fake PaletteColor', (): void => {
const fakeColor: PaletteColor = {
RGB: {R: 0, G: 0, B: 0},
HSL: {H: 0, S: 0, L: 0},
HEX: '#000000',
NAME: 'test fake color',
DISCRIMINATOR: Discriminators.PALETTE_COLOR
};

expect(Discriminator.isPaletteColor(fakeColor)).toBeTruthy();
});
});
});
25 changes: 25 additions & 0 deletions src/test/discriminator/discriminators.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (C) 2024 brittni and the polar bear LLC.
*
* This file is a part of brittni and the polar bear's Generative Art Library,
* which is released under the GNU Affero General Public License, Version 3.0.
* You may not use this file except in compliance with the license.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. See LICENSE or go to
* https://www.gnu.org/licenses/agpl-3.0.en.html for full license details.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
*/

import {Discriminators} from 'discriminator';

describe('discriminators tests', (): void => {
test('all discriminators are unique', (): void => {
const valuesSet: Set<string> = new Set<string>(Object.values(Discriminators))
expect(valuesSet.size).toBe(Object.values(Discriminators).length);
})
});
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"paths": {
"color": ["./src/main/color"],
"context": ["./src/main/p5"],
"discriminator": ["./src/main/discriminator"],
"map": ["./src/main/map"],
"math": ["./src/main/math"],
"palette": ["./src/main/color/palette"],
Expand Down

0 comments on commit f29aab3

Please sign in to comment.