forked from swc-project/swc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
colors.js
68 lines (68 loc) · 1.13 KB
/
colors.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
'use strict';
/**
* Extract red color out of a color integer:
*
* 0x00DEAD -> 0x00
*
* @param {Number} color
* @return {Number}
*/
function red( color )
{
return color >> 16;
}
/**
* Extract green out of a color integer:
*
* 0x00DEAD -> 0xDE
*
* @param {Number} color
* @return {Number}
*/
function green( color )
{
return ( color >> 8 ) & 0xFF;
}
/**
* Extract blue color out of a color integer:
*
* 0x00DEAD -> 0xAD
*
* @param {Number} color
* @return {Number}
*/
function blue( color )
{
return color & 0xFF;
}
/**
* Converts an integer containing a color such as 0x00DEAD to a hex
* string, such as '#00DEAD';
*
* @param {Number} int
* @return {String}
*/
function intToHex( int )
{
const mask = '#000000';
const hex = int.toString( 16 );
return mask.substring( 0, 7 - hex.length ) + hex;
}
/**
* Converts a hex string containing a color such as '#00DEAD' to
* an integer, such as 0x00DEAD;
*
* @param {Number} num
* @return {String}
*/
function hexToInt( hex )
{
return parseInt( hex.substring( 1 ), 16 );
}
module.exports = {
red,
green,
blue,
intToHex,
hexToInt,
};