UNPKG

4.37 kBJavaScriptView Raw
1import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
2import * as namedColors from 'css-color-names'; // eslint-disable-line import/extensions
3
4/**
5 * We're avoding importing these colors from @atlaskit/theme since we
6 * do not want to have react as a dependency of this package.
7 * TODO: Refactor this once tokenization by Core team is ready
8 * https://product-fabric.atlassian.net/browse/CS-908
9 */
10
11export var R50 = '#FFEBE6';
12export var R75 = '#FFBDAD';
13export var R100 = '#FF8F73';
14export var R300 = '#FF5630';
15export var R400 = '#DE350B';
16export var R500 = '#BF2600';
17export var Y50 = '#FFFAE6';
18export var Y75 = '#FFF0B3';
19export var Y200 = '#FFC400';
20export var Y400 = '#FF991F';
21export var Y500 = '#FF8B00';
22export var G50 = '#E3FCEF';
23export var G75 = '#ABF5D1';
24export var G200 = '#57D9A3';
25export var G300 = '#36B37E';
26export var G400 = '#00875A';
27export var G500 = '#006644';
28export var B50 = '#DEEBFF';
29export var B75 = '#B3D4FF';
30export var B100 = '#4C9AFF';
31export var B400 = '#0052CC';
32export var B500 = '#0747A6';
33export var N0 = '#FFFFFF';
34export var N20 = '#F4F5F7';
35export var N30 = '#EBECF0';
36export var N40 = '#DFE1E6';
37export var N50 = '#C1C7D0';
38export var N60 = '#B3BAC5';
39export var N80 = '#97A0AF';
40export var N90 = '#8993A4';
41export var N200 = '#6B778C';
42export var N300 = '#5E6C84';
43export var N300A = '#091E4224';
44export var N500 = '#42526E';
45export var N600 = '#758195';
46export var N800 = '#172B4D';
47export var N1000 = '#172B4D';
48export var P50 = '#EAE6FF';
49export var P75 = '#C0B6F2';
50export var P100 = '#998DD9';
51export var P300 = '#6554C0';
52export var P400 = '#5243AA';
53export var P500 = '#403294';
54export var T50 = '#E6FCFF';
55export var T75 = '#B3F5FF';
56export var T100 = '#79E2F2';
57export var T300 = '#00B8D9';
58export var T500 = '#008DA6';
59
60/**
61 * @return String with HEX-coded color.
62 */
63export function normalizeHexColor(color, defaultColor) {
64 if (!color) {
65 return null;
66 }
67
68 // Normalize to hex
69 color = color.trim().toLowerCase();
70 if (isHex(color)) {
71 // Normalize 3-hex to 6-hex colours
72 if (color.length === 4) {
73 color = color.split('').map(function (c) {
74 return c === '#' ? '#' : "".concat(c).concat(c);
75 }).join('');
76 }
77 } else if (isRgb(color)) {
78 return rgbToHex(color);
79 } else {
80 // http://dev.w3.org/csswg/css-color/#named-colors
81 if (color === 'default') {
82 return null;
83 } else if (namedColors.default && namedColors.default[color]) {
84 color = namedColors.default[color];
85 } else if (namedColors && namedColors[color]) {
86 color = namedColors[color];
87 } else {
88 return null;
89 }
90 }
91 if (color === defaultColor) {
92 return null;
93 }
94 return color;
95}
96
97/**
98 * Converts hex color format to rgb.
99 * Works well with full hex color format and shortcut as well.
100 *
101 * @param hex - hex color string (#xxx, or #xxxxxx)
102 */
103export function hexToRgb(color) {
104 if (!isHex(color)) {
105 return null;
106 }
107 var colorBits = color.substring(1).split('');
108 if (colorBits.length === 3) {
109 colorBits = [colorBits[0], colorBits[0], colorBits[1], colorBits[1], colorBits[2], colorBits[2]];
110 }
111 var rgb = Number("0x".concat(colorBits.join('')));
112 // eslint-disable-next-line no-bitwise
113 return "rgb(".concat(rgb >> 16 & 255, ",").concat(rgb >> 8 & 255, ",").concat(rgb & 255, ")");
114}
115
116/**
117 * Converts hex color format to rgba.
118 *
119 * @param hex - hex color string (#xxx, or #xxxxxx)
120 */
121export function hexToRgba(rawColor, alpha) {
122 var color = normalizeHexColor(rawColor);
123 if (!color) {
124 return null;
125 }
126 var hex2rgb = function hex2rgb(color) {
127 return color.match(/[a-z0-9]{2}/gi).map(function (hex) {
128 return parseInt(hex, 16);
129 });
130 };
131 return "rgba(".concat(hex2rgb(color).concat(alpha).join(','), ")");
132}
133export function rgbToHex(value) {
134 var matches = value.match(/(0?\.?\d{1,3})%?\b/g);
135 if (matches && matches.length >= 3) {
136 var _matches$map = matches.map(Number),
137 _matches$map2 = _slicedToArray(_matches$map, 3),
138 red = _matches$map2[0],
139 green = _matches$map2[1],
140 blue = _matches$map2[2];
141 return '#' + (blue | green << 8 | red << 16 | 1 << 24).toString(16).slice(1) // eslint-disable-line no-bitwise
142 ;
143 }
144
145 return null;
146}
147export function isRgb(color) {
148 return /rgba?\(/.test(color);
149}
150export function isHex(color) {
151 return /^#([A-Fa-f0-9]{3}){1,2}$/.test(color);
152}
\No newline at end of file