Skip to content
This repository has been archived by the owner on Jan 6, 2024. It is now read-only.

Commit

Permalink
#22 Add two concrete color builder classes
Browse files Browse the repository at this point in the history
- Update .eslintrc
- Add import path mapping to tsconfig.json
- Add parent class for classes dependent on p5 methods
- Add directory level exports for all folders
- Update imports in color.ts
- Update exports for color-factory.ts
- Update exports in range.ts
- Update imports and exports in mapping-color-factory.ts and random-color-factory.ts
- Add random methods
- Create color factory for colors created from a range of RGB values
- Create color factory for random black colors
- Create color factory for random blue colors
- Update sketch.ts
  • Loading branch information
blwatkins committed Dec 30, 2023
1 parent de69283 commit 2492ab8
Show file tree
Hide file tree
Showing 19 changed files with 378 additions and 22 deletions.
4 changes: 3 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
"no-useless-constructor": "off",
"@typescript-eslint/no-useless-constructor": "error",

"@typescript-eslint/no-inferrable-types": "off"
"@typescript-eslint/no-inferrable-types": "off",

"@typescript-eslint/class-literal-property-style": ["error", "getters"]
}
}
20 changes: 10 additions & 10 deletions src/common/color/color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,18 @@
* See the GNU Affero General Public License for more details.
*/

import PLib from 'p5';
import { P5Dependant, P5Lib } from "p5-lib";

class Color {
type P5Color = P5Lib.Color;

class Color extends P5Dependant {
private _red: number;
private _green: number;
private _blue: number;
private _alpha: number;

constructor(private readonly _p5: PLib, color?: PLib.Color) {
public constructor(p5: P5Lib, color?: P5Color) {
super(p5);
this._red = 0;
this._green = 0;
this._blue = 0;
Expand All @@ -34,11 +37,11 @@ class Color {
}
}

public get color(): PLib.Color {
public get color(): P5Color {
return this.p5.color(this.red, this.green, this.blue, this.alpha);
}

public set color(c: PLib.Color) {
public set color(c: P5Color) {
this.setColorValues(c);
}

Expand Down Expand Up @@ -74,16 +77,13 @@ class Color {
this._alpha = Math.floor(this.p5.constrain(a, 0, 255));
}

private setColorValues(color: PLib.Color): void {
private setColorValues(color: P5Color): void {
this.red = this.p5.red(color);
this.green = this.p5.green(color);
this.blue = this.p5.blue(color);
this.alpha = this.p5.alpha(color);
}

private get p5(): PLib {
return this._p5;
}
}

export { Color };
export default Color;
1 change: 1 addition & 0 deletions src/common/color/factory/color-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ interface ColorFactory {
get name(): string;
}

export { type ColorFactory };
export default ColorFactory;
22 changes: 22 additions & 0 deletions src/common/color/factory/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright (C) 2023 Brittni Watkins.
*
* This file is a part of brittni and the polar bear's Generative Art Project Template,
* 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 { type ColorFactory } from "./color-factory";
export { type RandomColorFactory } from "./random-color-factory";
export { type MappingColorFactory } from "./mapping-color-factory";

export * from "./rgb";
4 changes: 3 additions & 1 deletion src/common/color/factory/mapping-color-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
* See the GNU Affero General Public License for more details.
*/

import { Color } from "color";

import ColorFactory from "./color-factory";
import Color from "../color";

interface MappingColorFactory extends ColorFactory {
mapColor(value: number, minValue: number, maxValue: number, saturation: number, brightness: number): Color;
}

export { type MappingColorFactory };
export default MappingColorFactory;
4 changes: 3 additions & 1 deletion src/common/color/factory/random-color-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
* See the GNU Affero General Public License for more details.
*/

import { Color } from "color";

import ColorFactory from "./color-factory";
import Color from "../color";

interface RandomColorFactory extends ColorFactory {
getRandomColor(): Color;
}

export { type RandomColorFactory };
export default RandomColorFactory;
37 changes: 37 additions & 0 deletions src/common/color/factory/rgb/black-color-factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (C) 2023 Brittni Watkins.
*
* This file is a part of brittni and the polar bear's Generative Art Project Template,
* 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 { P5Lib } from "p5-lib";
import { randomInt } from "random";
import { Color } from "color";

import RGBRangeFactory from "./rgb-range-factory";

class BlackColorFactory extends RGBRangeFactory {
public constructor(p5: P5Lib) {
super(p5, 'black color factory');
}

public override getRandomColor(): Color {
this.p5.colorMode(this.p5.RGB, 255);
const gray: number = randomInt(0, 100);
return new Color(this.p5, this.p5.color(gray));
}
}

export { BlackColorFactory };
export default BlackColorFactory;
33 changes: 33 additions & 0 deletions src/common/color/factory/rgb/blue-color-factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (C) 2023 Brittni Watkins.
*
* This file is a part of brittni and the polar bear's Generative Art Project Template,
* 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 { P5Lib } from "p5-lib";
import { Range } from "range";

import RGBRangeFactory from "./rgb-range-factory";

class BlueColorFactory extends RGBRangeFactory {
public constructor(p5: P5Lib) {
const redRange: Range = new Range(0, 100);
const greenRange: Range = new Range(0, 100);
const blueRange: Range = new Range(100, 255);
super(p5, 'blue color factory', redRange, greenRange, blueRange);
}
}

export { BlueColorFactory };
export default BlueColorFactory;
20 changes: 20 additions & 0 deletions src/common/color/factory/rgb/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright (C) 2023 Brittni Watkins.
*
* This file is a part of brittni and the polar bear's Generative Art Project Template,
* 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 { RGBRangeFactory } from "./rgb-range-factory";
export { BlackColorFactory } from "./black-color-factory";
export { BlueColorFactory } from "./blue-color-factory";
73 changes: 73 additions & 0 deletions src/common/color/factory/rgb/rgb-range-factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright (C) 2023 Brittni Watkins.
*
* This file is a part of brittni and the polar bear's Generative Art Project Template,
* 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 { P5Dependant, P5Lib } from "p5-lib";
import { Color } from "color";
import { Range } from "range";
import { randomInt } from "random";

import RandomColorFactory from "../random-color-factory";

abstract class RGBRangeFactory extends P5Dependant implements RandomColorFactory {

protected constructor(p5: P5Lib, _name: string);
protected constructor(p5: P5Lib,
_name: string,
_redRange: Range,
_greenRange: Range,
_blueRange: Range);
protected constructor(p5: P5Lib,
private readonly _name: string,
private readonly _redRange?: Range,
private readonly _greenRange?: Range,
private readonly _blueRange?: Range) {
super(p5);
}

public get name(): string {
return this._name;
}

public getRandomColor(): Color {
this.p5.colorMode(this.p5.RGB, 255);
let color: Color = new Color(this.p5);

if (this.redRange && this.greenRange && this.blueRange) {
const r: number = randomInt(this.redRange.low, this.redRange.high);
const g: number = randomInt(this.greenRange.low, this.greenRange.high);
const b: number = randomInt(this.blueRange.low, this.blueRange.high);
color = new Color(this.p5, this.p5.color(r, g, b));
}

return color;
}

private get redRange(): Range | undefined {
return this._redRange;
}

private get greenRange(): Range | undefined {
return this._greenRange;
}

private get blueRange(): Range | undefined {
return this._blueRange;
}
}

export { RGBRangeFactory };
export default RGBRangeFactory;
18 changes: 18 additions & 0 deletions src/common/color/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright (C) 2023 Brittni Watkins.
*
* This file is a part of brittni and the polar bear's Generative Art Project Template,
* 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 { Color } from "./color";
22 changes: 22 additions & 0 deletions src/common/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright (C) 2023 Brittni Watkins.
*
* This file is a part of brittni and the polar bear's Generative Art Project Template,
* 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 "color";
export * from "color/factory";
export * from "p5-lib";
export * from "random";
export * from "range";
21 changes: 21 additions & 0 deletions src/common/p5/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright (C) 2023 Brittni Watkins.
*
* This file is a part of brittni and the polar bear's Generative Art Project Template,
* 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 P5Lib from "p5";

export { P5Lib };
export * from "./p5-dependant";
30 changes: 30 additions & 0 deletions src/common/p5/p5-dependant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (C) 2023 Brittni Watkins.
*
* This file is a part of brittni and the polar bear's Generative Art Project Template,
* 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 P5Lib from "p5";

abstract class P5Dependant {
protected constructor(private readonly _p5: P5Lib) {
}

protected get p5(): P5Lib {
return this._p5;
}
}

export { P5Dependant };
export default P5Dependant;
Loading

0 comments on commit 2492ab8

Please sign in to comment.