-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Code refactoring, create constants for color types
- Loading branch information
Showing
6 changed files
with
165 additions
and
99 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module.exports = { | ||
DEFAULT_HEX_ALPHA: 'ff', | ||
DEFAULT_ALPHA: 1, | ||
HEX_COLOR: 'hex', | ||
RGB_COLOR: 'rgb', | ||
HSL_COLOR: 'hsl', | ||
KEYWORD_COLOR: 'keyword', | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,118 @@ | ||
const { | ||
parseHEXAColor, | ||
getRGBColorStr, | ||
getHSLColorStr, | ||
getHEXColorStr, | ||
} = require('./utils'); | ||
const { | ||
DEFAULT_HEX_ALPHA, | ||
DEFAULT_ALPHA, | ||
HEX_COLOR, | ||
RGB_COLOR, | ||
HSL_COLOR, | ||
KEYWORD_COLOR, | ||
} = require('./constants'); | ||
|
||
const convertingHEXColor = (node, options) => { | ||
const colorObj = parseHEXAColor(node.value); | ||
|
||
if (options.outputColorFormat === RGB_COLOR) { | ||
node.value = options.alwaysAlpha || colorObj.hexAlpha !== DEFAULT_HEX_ALPHA | ||
? getRGBColorStr(HEX_COLOR, colorObj.hexColor, colorObj.hexAlpha) | ||
: getRGBColorStr(HEX_COLOR, colorObj.hexColor); | ||
} else if (options.outputColorFormat === HSL_COLOR) { | ||
node.value = options.alwaysAlpha || colorObj.hexAlpha !== DEFAULT_HEX_ALPHA | ||
? getHSLColorStr(HEX_COLOR, colorObj.hexColor, colorObj.hexAlpha) | ||
: getHSLColorStr(HEX_COLOR, colorObj.hexColor); | ||
} | ||
|
||
return node; | ||
}; | ||
|
||
const convertingRGBColor = (node, options) => { | ||
const newNode = node.clone({ type: 'word' }); | ||
const [r, , g, , b, , a] = node.nodes; | ||
|
||
if (options.outputColorFormat === HEX_COLOR) { | ||
newNode.value = getHEXColorStr( | ||
RGB_COLOR, | ||
[+r.value, +g.value, +b.value], | ||
((a && +a.value !== DEFAULT_ALPHA && +a.value)), | ||
); | ||
} else if (options.outputColorFormat === HSL_COLOR) { | ||
newNode.value = getHSLColorStr( | ||
RGB_COLOR, | ||
[+r.value, +g.value, +b.value], | ||
((a && +a.value) || (options.alwaysAlpha && DEFAULT_ALPHA)), | ||
); | ||
} else if (options.outputColorFormat === RGB_COLOR) { | ||
newNode.value = getRGBColorStr( | ||
RGB_COLOR, | ||
[+r.value, +g.value, +b.value], | ||
(a && +a.value) || (options.alwaysAlpha && DEFAULT_ALPHA), | ||
); | ||
} | ||
|
||
node.replaceWith(newNode); | ||
|
||
return node; | ||
}; | ||
|
||
const convertingHSLColor = (node, options) => { | ||
const newNode = node.clone({ type: 'word' }); | ||
const [h, , s, , l, , a] = node.nodes; | ||
|
||
if (options.outputColorFormat === HEX_COLOR) { | ||
newNode.value = getHEXColorStr( | ||
HSL_COLOR, | ||
[+h.value, +s.value, +l.value], | ||
((a && +a.value !== DEFAULT_ALPHA && +a.value)), | ||
); | ||
} else if (options.outputColorFormat === RGB_COLOR) { | ||
newNode.value = getRGBColorStr( | ||
HSL_COLOR, | ||
[+h.value, +s.value, +l.value], | ||
((a && +a.value) || (options.alwaysAlpha && DEFAULT_ALPHA)), | ||
); | ||
} else if (options.outputColorFormat === HSL_COLOR) { | ||
newNode.value = getHSLColorStr( | ||
HSL_COLOR, | ||
[+h.value, +s.value, +l.value], | ||
(a && +a.value) || (options.alwaysAlpha && DEFAULT_ALPHA), | ||
); | ||
} | ||
|
||
node.replaceWith(newNode); | ||
|
||
return node; | ||
}; | ||
|
||
const convertingKeywordColor = (node, options) => { | ||
if (options.outputColorFormat === HEX_COLOR) { | ||
node.value = getHEXColorStr( | ||
KEYWORD_COLOR, | ||
node.value, | ||
); | ||
} else if (options.outputColorFormat === RGB_COLOR) { | ||
node.value = getRGBColorStr( | ||
KEYWORD_COLOR, | ||
node.value, | ||
options.alwaysAlpha && DEFAULT_ALPHA, | ||
); | ||
} else if (options.outputColorFormat === HSL_COLOR) { | ||
node.value = getHSLColorStr( | ||
KEYWORD_COLOR, | ||
node.value, | ||
options.alwaysAlpha && DEFAULT_ALPHA, | ||
); | ||
} | ||
|
||
return node; | ||
}; | ||
|
||
module.exports = { | ||
convertingHEXColor, | ||
convertingRGBColor, | ||
convertingHSLColor, | ||
convertingKeywordColor, | ||
}; |
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