-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #67 from vunguyentuan/52-support-for-color-functio…
…nal-notation 52 support for color functional notation
- Loading branch information
Showing
3 changed files
with
13 additions
and
217 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
214 changes: 3 additions & 211 deletions
214
packages/css-variables-language-server/src/utils/isColor.ts
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,217 +1,9 @@ | ||
// borrow from https://github.com/princejwesley/is-css-color/blob/master/index.js | ||
'use strict'; | ||
|
||
//every string I match against are lowercase | ||
const HEX_PATTERN = /^#(?:[a-f0-9]{3})?(?:[a-f0-9]{3})$/; | ||
// css color names + initial + inherit + currentColor + transparent | ||
const CSS_COLOR_NAMES = [ | ||
'aliceblue', | ||
'antiquewhite', | ||
'aqua', | ||
'aquamarine', | ||
'azure', | ||
'beige', | ||
'bisque', | ||
'black', | ||
'blanchedalmond', | ||
'blue', | ||
'blueviolet', | ||
'brown', | ||
'burlywood', | ||
'cadetblue', | ||
'chartreuse', | ||
'chocolate', | ||
'coral', | ||
'cornflowerblue', | ||
'cornsilk', | ||
'crimson', | ||
'currentColor', | ||
'cyan', | ||
'darkblue', | ||
'darkcyan', | ||
'darkgoldenrod', | ||
'darkgray', | ||
'darkgreen', | ||
'darkgrey', | ||
'darkkhaki', | ||
'darkmagenta', | ||
'darkolivegreen', | ||
'darkorange', | ||
'darkorchid', | ||
'darkred', | ||
'darksalmon', | ||
'darkseagreen', | ||
'darkslateblue', | ||
'darkslategray', | ||
'darkslategrey', | ||
'darkturquoise', | ||
'darkviolet', | ||
'deeppink', | ||
'deepskyblue', | ||
'dimgray', | ||
'dimgrey', | ||
'dodgerblue', | ||
'firebrick', | ||
'floralwhite', | ||
'forestgreen', | ||
'fuchsia', | ||
'gainsboro', | ||
'ghostwhite', | ||
'gold', | ||
'goldenrod', | ||
'gray', | ||
'green', | ||
'greenyellow', | ||
'grey', | ||
'honeydew', | ||
'hotpink', | ||
'indianred', | ||
'indigo', | ||
'inherit', | ||
'initial', | ||
'ivory', | ||
'khaki', | ||
'lavender', | ||
'lavenderblush', | ||
'lawngreen', | ||
'lemonchiffon', | ||
'lightblue', | ||
'lightcoral', | ||
'lightcyan', | ||
'lightgoldenrodyellow', | ||
'lightgray', | ||
'lightgreen', | ||
'lightgrey', | ||
'lightpink', | ||
'lightsalmon', | ||
'lightseagreen', | ||
'lightskyblue', | ||
'lightslategray', | ||
'lightslategrey', | ||
'lightsteelblue', | ||
'lightyellow', | ||
'lime', | ||
'limegreen', | ||
'linen', | ||
'magenta', | ||
'maroon', | ||
'mediumaquamarine', | ||
'mediumblue', | ||
'mediumorchid', | ||
'mediumpurple', | ||
'mediumseagreen', | ||
'mediumslateblue', | ||
'mediumspringgreen', | ||
'mediumturquoise', | ||
'mediumvioletred', | ||
'midnightblue', | ||
'mintcream', | ||
'mistyrose', | ||
'moccasin', | ||
'navajowhite', | ||
'navy', | ||
'oldlace', | ||
'olive', | ||
'olivedrab', | ||
'orange', | ||
'orangered', | ||
'orchid', | ||
'palegoldenrod', | ||
'palegreen', | ||
'paleturquoise', | ||
'palevioletred', | ||
'papayawhip', | ||
'peachpuff', | ||
'peru', | ||
'pink', | ||
'plum', | ||
'powderblue', | ||
'purple', | ||
'rebeccapurple', | ||
'red', | ||
'rosybrown', | ||
'royalblue', | ||
'saddlebrown', | ||
'salmon', | ||
'sandybrown', | ||
'seagreen', | ||
'seashell', | ||
'sienna', | ||
'silver', | ||
'skyblue', | ||
'slateblue', | ||
'slategray', | ||
'slategrey', | ||
'snow', | ||
'springgreen', | ||
'steelblue', | ||
'tan', | ||
'teal', | ||
'thistle', | ||
'tomato', | ||
'transparent', | ||
'turquoise', | ||
'violet', | ||
'wheat', | ||
'white', | ||
'whitesmoke', | ||
'yellow', | ||
'yellowgreen', | ||
]; | ||
|
||
const PREFIX = '^(rgb|hsl)(a?)\\s*\\('; | ||
const VALUE = '\\s*([-+]?\\d+%?)\\s*'; | ||
const ALPHA = '(?:,\\s*([-+]?(?:(?:\\d+(?:.\\d+)?)|(?:.\\d+))\\s*))?'; | ||
const SUFFIX = '\\)$'; | ||
const RGB_HSL_PATTERN = new RegExp(PREFIX + VALUE + ',' + VALUE + ',' + VALUE + ALPHA + SUFFIX); | ||
|
||
const NUM_TYPE = 1; | ||
const PERCENTAGE_TYPE = 2; | ||
const ERROR_TYPE = NUM_TYPE & PERCENTAGE_TYPE; | ||
import * as culori from 'culori'; | ||
|
||
const isColor = (str: string) => { | ||
function getColorType(token: string) { | ||
return token.indexOf('%') !== -1 ? PERCENTAGE_TYPE : NUM_TYPE; | ||
} | ||
|
||
if(!str || typeof str !== 'string') { | ||
return false; | ||
} | ||
|
||
const color = str.replace(/^\s+|\s+$/g, '').toLocaleLowerCase(); | ||
|
||
// named colors or hex code | ||
if((CSS_COLOR_NAMES.indexOf(color) !== -1) || HEX_PATTERN.test(color)) { | ||
return true; | ||
} | ||
|
||
const result = color.match(RGB_HSL_PATTERN); | ||
if(result) { | ||
const flavor = result[1]; | ||
const alpha = result[2]; | ||
const rh = result[3]; | ||
const gs = result[4]; | ||
const bl = result[5]; | ||
const a = result[6]; | ||
|
||
// alpha test | ||
if((alpha === 'a' && !a) || (a && alpha === '')) { | ||
return false; | ||
} | ||
|
||
// hsl | ||
if(flavor === 'hsl') { | ||
if(getColorType(rh) !== NUM_TYPE) { | ||
return false; | ||
} | ||
return (getColorType(gs) & getColorType(bl)) === PERCENTAGE_TYPE; | ||
} | ||
|
||
// rgb | ||
return (getColorType(rh) & getColorType(gs) & getColorType(bl)) !== ERROR_TYPE; | ||
} | ||
const colorTemp = culori.parse(str); | ||
|
||
return false; | ||
return !!colorTemp; | ||
}; | ||
|
||
export default isColor; |