generated from unjs/template
-
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.
- Loading branch information
1 parent
73bd59e
commit 34667ed
Showing
3 changed files
with
44 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import type { MyColorPlugin, MyColor, MyColorFn } from "../.."; | ||
|
||
interface OhColorClass extends MyColor { | ||
getLuminance: () => number; | ||
} | ||
|
||
interface OhColorFactory extends MyColorFn { | ||
(...args: unknown[]): OhColorClass; | ||
} | ||
|
||
const f = (x: number) => { | ||
const channel = x / 255; | ||
return channel <= 0.040_45 | ||
? channel / 12.92 | ||
: Math.pow(((channel + 0.055) / 1.055), 2.4); | ||
} | ||
|
||
/** | ||
* Returns a number (float) representing the luminance of a color. | ||
*/ | ||
export const getLuminance: MyColorPlugin<OhColorFactory> = (_, c, cf) => { | ||
const proto = c.prototype as OhColorClass; | ||
const getRGB = proto.rgba; | ||
proto.getLuminance = function() { | ||
const [r, g, b] = getRGB.bind(this)(); | ||
return 0.2126 * f(r) + 0.7152 * f(g) + 0.0722 * f(b); | ||
} | ||
|
||
return cf as OhColorFactory; | ||
} | ||
|
||
export default getLuminance |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { inputHex } from "./inputHex"; | ||
export { outputHex } from "./outputHex"; | ||
export { inputNamed } from './inputNamed'; | ||
export { getLuminance } from './getLuminance'; |
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,11 @@ | ||
import { expect, it, describe } from "vitest"; | ||
import { mycolor } from "../../src"; | ||
import { getLuminance } from "../../src/plugin" | ||
|
||
describe("mycolor plugin: getLuminance", () => { | ||
it("should get luminance", () => { | ||
const mycolor2 = mycolor.extend(getLuminance); | ||
|
||
expect(mycolor2(255, 255, 0, 1).getLuminance()).toBe(0.9278); | ||
}); | ||
}) |