Skip to content

Commit

Permalink
add luminance calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Kühnapfel committed Jun 23, 2022
1 parent 997edac commit ce5e4b2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added this changelog
- Added README
- Post install script to automatically transpile typescript code
- `luminance` attribute, showing the relative lumincance as a float (0..1)
- `calculateLuminance` method, calculating the relative luminance according to diffrent lumincane algorithms
- `LuminanceAlgorithm` enum
- `LINEAR_RGB = 0` [Standard algorithm to calculate relative luminance for linear rgb](https://en.wikipedia.org/wiki/Relative_luminance#Relative_luminance_and_%22gamma_encoded%22_colorspaces)
- `W3C_AERT = 1` [Algorithm to calculate ralative luminance according to W3Cs Working Draft for AERT](https://www.w3.org/TR/AERT/#color-contrast)
- `HSP = 2` [Algorithm to calculate brightness in HSP color space](https://alienryderflex.com/hsp.html)

### Changed
- Added dist/ directory to `.gitignore`
Expand Down
25 changes: 25 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ export default class ColorConvert {
get hsl (): string { return this.toHsl() }
get hsla (): string { return this.toHsla() }

get luminance (): number { return this.calculateLuminance() }

constructor (input: string) {
if (ColorConvert.isRgbString(input)) {
[this.#red, this.#green, this.#blue, this.#alpha] = ColorConvert.#parseRgb(input)
Expand Down Expand Up @@ -240,6 +242,17 @@ export default class ColorConvert {
return `hsl(${this.#hue2UnitString(hueUnit)}, ${ColorConvert.#roundTwoDecimals(this.saturation * 100)}%, ${ColorConvert.#roundTwoDecimals(this.lightness * 100)}%, ${ColorConvert.#roundTwoDecimals(this.alpha)})`
}

calculateLuminance (algorithm: LuminanceAlgorithm = LuminanceAlgorithm.LINEAR_RGB): number {
switch (algorithm) {
case LuminanceAlgorithm.LINEAR_RGB:
return (0.2126 * this.red + 0.7152 * this.green + 0.0722 * this.blue) / 0xff
case LuminanceAlgorithm.W3C_AERT:
return (0.299 * this.red + 0.587 * this.green + 0.114 * this.blue) / 0xff
case LuminanceAlgorithm.HSP:
return Math.sqrt(0.299 * this.red ** 2 + 0.587 * this.green ** 2 + 0.114 * this.blue ** 2) / 0xff
}
}

#getHsl (): [number, number, number] {
const [red, green, blue] = [
this.red / 0xff,
Expand Down Expand Up @@ -388,3 +401,15 @@ export default class ColorConvert {
return 'ColorConvert'
}
}

/**
* Enum that corresponds to diffrent luminance calculating algorithms
* @property LINEAR_RGB - {@link https://en.wikipedia.org/wiki/Relative_luminance#Relative_luminance_and_%22gamma_encoded%22_colorspaces|Standard algorithm to calculate relative luminance for linear rgb}
* @property W3C_AERT - {@link https://www.w3.org/TR/AERT/#color-contrast|Algorithm to calculate ralative luminance according to W3Cs Working Draft for AERT}
* @property HSP - {@link https://alienryderflex.com/hsp.html|Algorithm to calculate brightness in HSP color space}
*/
export enum LuminanceAlgorithm {
LINEAR_RGB,
W3C_AERT,
HSP
}

0 comments on commit ce5e4b2

Please sign in to comment.