-
Notifications
You must be signed in to change notification settings - Fork 180
Open
Labels
bugSomething isn't workingSomething isn't workinglibrary: typescriptquick(Label used internally)(Label used internally)
Description
The argbFromHex
function does not parse hex values correctly when the hex value is 8 characters long (excluding the #
). It incorrectly assumes the last two characters are the blue channel when it's actually the alpha channel.
Example: 50% opacity as represented by the 80
in the input (black)
Input: #80 (equivalent to #7f7f7f)
Incorrect Output: 4278190208 (equivalent to #80) ... a blue
material-color-utilities/typescript/utils/string_utils.ts
Lines 50 to 78 in 9e67d5e
export const argbFromHex = (hex: string) => { | |
hex = hex.replace('#', ''); | |
const isThree = hex.length === 3; | |
const isSix = hex.length === 6; | |
const isEight = hex.length === 8; | |
if (!isThree && !isSix && !isEight) { | |
throw new Error('unexpected hex ' + hex); | |
} | |
let r = 0; | |
let g = 0; | |
let b = 0; | |
if (isThree) { | |
r = parseIntHex(hex.slice(0, 1).repeat(2)); | |
g = parseIntHex(hex.slice(1, 2).repeat(2)); | |
b = parseIntHex(hex.slice(2, 3).repeat(2)); | |
} else if (isSix) { | |
r = parseIntHex(hex.slice(0, 2)); | |
g = parseIntHex(hex.slice(2, 4)); | |
b = parseIntHex(hex.slice(4, 6)); | |
} else if (isEight) { | |
r = parseIntHex(hex.slice(2, 4)); | |
g = parseIntHex(hex.slice(4, 6)); | |
b = parseIntHex(hex.slice(6, 8)); | |
} | |
return ( | |
((255 << 24) | ((r & 0x0ff) << 16) | ((g & 0x0ff) << 8) | (b & 0x0ff)) >>> | |
0); | |
}; |
Grohden
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't workinglibrary: typescriptquick(Label used internally)(Label used internally)