Skip to content

Instantly share code, notes, and snippets.

@devethan
Last active April 8, 2021 05:34
Show Gist options
  • Save devethan/0597f9036a6d36983a86f90ca04f0a4a to your computer and use it in GitHub Desktop.
Save devethan/0597f9036a6d36983a86f90ca04f0a4a to your computer and use it in GitHub Desktop.
generateOpacityToHexString
const hexOpacityList = [
'00',
'03',
'05',
'08',
'0A',
'0D',
'0F',
'12',
'14',
'17',
'1A',
'1C',
'1F',
'21',
'24',
'26',
'29',
'2B',
'2E',
'30',
'33',
'36',
'38',
'3B',
'3D',
'40',
'42',
'45',
'47',
'4A',
'4D',
'4F',
'52',
'54',
'57',
'59',
'5C',
'5E',
'61',
'63',
'66',
'69',
'6B',
'6E',
'70',
'73',
'75',
'78',
'7A',
'7D',
'80',
'82',
'85',
'87',
'8A',
'8C',
'8F',
'91',
'94',
'96',
'99',
'9C',
'9E',
'A1',
'A3',
'A6',
'A8',
'AB',
'AD',
'B0',
'B3',
'B5',
'B8',
'BA',
'BD',
'BF',
'C2',
'C4',
'C7',
'C9',
'CC',
'CF',
'D1',
'D4',
'D6',
'D9',
'DB',
'DE',
'E0',
'E3',
'E6',
'E8',
'EB',
'ED',
'F0',
'F2',
'F5',
'F7',
'FA',
'FC',
'FF',
];
export const generateOpacityToHexString = (
hex: string,
opacity: number,
): string => {
try {
if (!Number.isInteger(opacity) || opacity < 0 || opacity > 100)
throw new Error(
`${opacity} is not expected value of opacity. Please put a value between 0 and 100 of integer`,
);
return `${hex}${hexOpacityList[opacity]}`;
} catch (error) {
console.error(error);
return hex;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment