-
Notifications
You must be signed in to change notification settings - Fork 500
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[desktop] handle three digit hex color codes
- Loading branch information
1 parent
d7b7940
commit 53481e7
Showing
4 changed files
with
45 additions
and
49 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,42 @@ | ||
//@flow | ||
|
||
import {assertMainOrNodeBoot} from "../../api/common/Env" | ||
import {ProgrammingError} from "../../api/common/error/ProgrammingError" | ||
import {assert} from "../../api/common/utils/Utils" | ||
|
||
assertMainOrNodeBoot() | ||
|
||
// 3 or 6 digit hex color codes | ||
export const VALID_HEX_CODE_FORMAT: RegExp = new RegExp("^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$") | ||
|
||
export function isColorLight(c: string): boolean { | ||
if (c[0] !== "#" || c.length !== 7) { | ||
throw new ProgrammingError("Invalid color format: " + c) | ||
} | ||
const rgb = parseInt(c.slice(1), 16); // convert rrggbb to decimal | ||
const r = (rgb >> 16) & 0xff // extract red | ||
const g = (rgb >> 8) & 0xff // extract green | ||
const b = (rgb >> 0) & 0xff // extract blue | ||
|
||
const {r, g, b} = hexToRgb(c) | ||
|
||
// Counting the perceptive luminance | ||
// human eye favors green color... | ||
const a = 1 - (0.299 * r + 0.587 * g + 0.114 * b) / 255 | ||
return a < 0.5 | ||
} | ||
|
||
/** | ||
* We use the alpha channel instead of using opacity for fading colors. Opacity changes are slow on mobile devices as they | ||
* effect the whole tree of the dom element with changing opacity. | ||
* | ||
* See http://stackoverflow.com/a/14677373 for a more detailed explanation. | ||
*/ | ||
export function hexToRgb(hexColor: string): {r: number, g: number, b: number} { | ||
if (!hexColor.startsWith("#")) { | ||
throw new Error("Illegal color definition: " + hexColor) | ||
} | ||
const withoutHash = hexColor.substring(1) | ||
let components | ||
if (withoutHash.length === 6) { | ||
components = { | ||
r: withoutHash.slice(0, 2), | ||
g: withoutHash.slice(2, 4), | ||
b: withoutHash.slice(4, 6), | ||
} | ||
} else if (withoutHash.length === 3) { | ||
components = { | ||
r: withoutHash[0] + withoutHash[0], | ||
g: withoutHash[1] + withoutHash[1], | ||
b: withoutHash[2] + withoutHash[2], | ||
} | ||
} else { | ||
throw new Error("Illegal color definition: " + hexColor) | ||
} | ||
const rgb = { | ||
r: parseInt(components.r, 16), | ||
g: parseInt(components.g, 16), | ||
b: parseInt(components.b, 16), | ||
} | ||
if (isNaN(rgb.r) || isNaN(rgb.g) || isNaN(rgb.b)) { | ||
throw new Error("Illegal color definition: " + hexColor) | ||
export function hexToRgb(colorCode: string): {r: number, g: number, b: number} { | ||
|
||
assert(VALID_HEX_CODE_FORMAT.test(colorCode), "Invalid color code: " + colorCode) | ||
|
||
let hexWithoutHash = colorCode.slice(1) | ||
if (hexWithoutHash.length === 3) { | ||
hexWithoutHash = Array.from(hexWithoutHash).reduce((acc, cur) => `${acc}${cur}${cur}`, "") // convert from 3 to 6 digits by duplicating each digit | ||
} | ||
return rgb | ||
|
||
const rgb = parseInt(hexWithoutHash, 16); // convert rrggbb to decimal | ||
|
||
const r = (rgb >> 16) & 0xff // extract red | ||
const g = (rgb >> 8) & 0xff // extract green | ||
const b = (rgb >> 0) & 0xff // extract blue | ||
|
||
|
||
return {r, g, b} | ||
} | ||
|
||
export function rgbToHex(color: {r: number, g: number, b: number}): string { | ||
return "#" + ((1 << 24) + (color.r << 16) + (color.g << 8) + color.b).toString(16).slice(1); | ||
} | ||
} |
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