-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from EricRovell/harmonies
Harmonies plugin implementation
- Loading branch information
Showing
6 changed files
with
143 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import type { Plugin } from "@types"; | ||
|
||
export type Harmony = | ||
| "analogous" | ||
| "complimentary" | ||
| "rectangle" | ||
| "tetradic" | ||
| "triadic" | ||
| "splitcomplimentary"; | ||
|
||
declare module "blossom" { | ||
interface Blossom { | ||
/** | ||
* Returns an array of harmony colors as `blossom` instances. | ||
*/ | ||
harmonies(type: Harmony): Blossom[]; | ||
} | ||
} | ||
|
||
export const pluginHarmonies: Plugin = (BaseClass): void => { | ||
/** | ||
* All harmony colors are just hue-shifted colors of particular angles. | ||
*/ | ||
const hueShifts: Record<Harmony, number[]> = { | ||
analogous: [ -30, 0, 30 ], | ||
complimentary: [ 0, 180 ], | ||
rectangle: [ 0, 60, 180, 240 ], | ||
tetradic: [ 0, 90, 180, 270 ], | ||
triadic: [ 0, 120, 240 ], | ||
splitcomplimentary: [ 0, 150, 210 ] | ||
}; | ||
|
||
BaseClass.prototype.harmonies = function(type = "complimentary") { | ||
return hueShifts[type].map(shift => this.rotate(shift)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { blossom } from "@/blossom"; | ||
import { extend } from "@/extend"; | ||
import { pluginHarmonies } from "@plugins/harmonies"; | ||
|
||
describe("Harmony colors plugin", () => { | ||
extend([ pluginHarmonies ]); | ||
|
||
const color = blossom("#FF0000"); | ||
|
||
it("Generates analogous colors", () => { | ||
expect(color.harmonies("analogous").map(value => value.hex)).toEqual([ | ||
"#FF0080", "#FF0000", "#FF8000" | ||
]); | ||
}); | ||
it("Generates complimentary colors", () => { | ||
expect(color.harmonies("complimentary").map(value => value.hex)).toEqual([ | ||
"#FF0000", "#00FFFF" | ||
]); | ||
}); | ||
it("Generates rectangle colors", () => { | ||
expect(color.harmonies("rectangle").map(value => value.hex)).toEqual([ | ||
"#FF0000", "#FFFF00", "#00FFFF", "#0000FF" | ||
]); | ||
}); | ||
it("Generates tetradic colors", () => { | ||
expect(color.harmonies("tetradic").map(value => value.hex)).toEqual([ | ||
"#FF0000", "#80FF00", "#00FFFF", "#8000FF" | ||
]); | ||
}); | ||
it("Generates triadic colors", () => { | ||
expect(color.harmonies("triadic").map(value => value.hex)).toEqual([ | ||
"#FF0000", "#00FF00", "#0000FF" | ||
]); | ||
}); | ||
it("Generates splitcomplimentary colors", () => { | ||
expect(color.harmonies("splitcomplimentary").map(value => value.hex)).toEqual([ | ||
"#FF0000", "#00FF80", "#0080FF" | ||
]); | ||
}); | ||
}); |