Skip to content

Commit e320719

Browse files
MoOxromainmenke
authored andcommitted
Initial release from postcss-color
Ref postcss/postcss-color#4
1 parent d0483d4 commit e320719

File tree

13 files changed

+422
-0
lines changed

13 files changed

+422
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# editorconfig.org
2+
root = true
3+
4+
[*]
5+
end_of_line = lf
6+
charset = utf-8
7+
trim_trailing_whitespace = true
8+
insert_final_newline = true
9+
indent_style = space
10+
indent_size = 2
11+
12+
[*.md]
13+
trim_trailing_whitespace = false
14+
15+
[Makefile]
16+
indent_style = tab
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
test/fixtures/*.actual.css
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
{
2+
"excludeFiles": [
3+
"node_modules/**"
4+
],
5+
"fileExtensions": [
6+
".js"
7+
],
8+
"requireCurlyBraces": [
9+
"if",
10+
"else",
11+
"for",
12+
"while",
13+
"do",
14+
"try",
15+
"catch"
16+
],
17+
"requireSpaceAfterKeywords": [
18+
"if",
19+
"else",
20+
"for",
21+
"while",
22+
"do",
23+
"switch",
24+
"return",
25+
"try",
26+
"catch"
27+
],
28+
"requireSpaceBeforeBlockStatements": true,
29+
"requireParenthesesAroundIIFE": true,
30+
"requireSpacesInConditionalExpression": {
31+
"afterTest": true,
32+
"beforeConsequent": true,
33+
"afterConsequent": true,
34+
"beforeAlternate": true
35+
},
36+
"requireSpacesInFunctionExpression": {
37+
"beforeOpeningCurlyBrace": true
38+
},
39+
"disallowSpacesInFunctionExpression": {
40+
"beforeOpeningRoundBrace": true
41+
},
42+
"disallowMultipleVarDecl": true,
43+
"requireBlocksOnNewline": 1,
44+
"disallowPaddingNewlinesInBlocks": true,
45+
"disallowEmptyBlocks": true,
46+
"disallowSpacesInsideObjectBrackets": true,
47+
"disallowSpacesInsideArrayBrackets": true,
48+
"disallowSpacesInsideParentheses": true,
49+
"disallowQuotedKeysInObjects": "allButReserved",
50+
"disallowSpaceAfterObjectKeys": true,
51+
"requireCommaBeforeLineBreak": true,
52+
"requireOperatorBeforeLineBreak": [
53+
"?",
54+
"+",
55+
"-",
56+
"/",
57+
"*",
58+
"=",
59+
"==",
60+
"===",
61+
"!=",
62+
"!==",
63+
">",
64+
">=",
65+
"<",
66+
"<="
67+
],
68+
"disallowSpaceAfterPrefixUnaryOperators": [
69+
"++",
70+
"--",
71+
"+",
72+
"-",
73+
"~",
74+
"!"
75+
],
76+
"disallowSpaceBeforePostfixUnaryOperators": [
77+
"++",
78+
"--"
79+
],
80+
"requireSpaceBeforeBinaryOperators": [
81+
"+",
82+
"-",
83+
"/",
84+
"*",
85+
"=",
86+
"==",
87+
"===",
88+
"!=",
89+
"!=="
90+
],
91+
"requireSpaceAfterBinaryOperators": [
92+
"+",
93+
"-",
94+
"/",
95+
"*",
96+
"=",
97+
"==",
98+
"===",
99+
"!=",
100+
"!=="
101+
],
102+
"disallowImplicitTypeConversion": [
103+
"numeric",
104+
"boolean",
105+
"binary",
106+
"string"
107+
],
108+
"requireCamelCaseOrUpperCaseIdentifiers": "ignoreProperties",
109+
"disallowKeywords": [
110+
"with"
111+
],
112+
"disallowMultipleLineStrings": true,
113+
"validateQuoteMarks": "\"",
114+
"validateIndentation": 2,
115+
"disallowMixedSpacesAndTabs": true,
116+
"disallowTrailingWhitespace": true,
117+
"requireKeywordsOnNewLine": [
118+
"else"
119+
],
120+
"requireLineFeedAtFileEnd": true,
121+
"requireCapitalizedConstructors": true,
122+
"safeContextKeyword": "that",
123+
"requireDotNotation": true,
124+
"validateJSDoc": {
125+
"checkParamNames": true,
126+
"checkRedundantParams": true,
127+
"requireParamTypes": true
128+
},
129+
"requireSpaceAfterLineComment": true
130+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"newcap": false,
3+
"undef": true,
4+
"unused": true,
5+
"asi": true,
6+
"esnext": true,
7+
"node": true,
8+
"browser": true
9+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
language: node_js
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# 1.0.0 - 2014-10-04
2+
3+
Initial release from [postcss-color](https://github.com/postcss/postcss-color)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2014 Maxime Thirouin
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# postcss-color-hex-alpha [![Build Status](https://travis-ci.org/postcss/postcss-color-hex-alpha.png)](https://travis-ci.org/postcss/postcss-color-hex-alpha)
2+
3+
> [PostCSS](https://github.com/postcss/postcss) plugin to transform [W3C RGBA hexadecimal notations (#RRGGBBAA or #RGBA)](http://dev.w3.org/csswg/css-color/#hex-notation) to more compatible CSS (rgba()).
4+
5+
## Installation
6+
7+
```bash
8+
$ npm install postcss-color-hex-alpha
9+
```
10+
11+
## Usage
12+
13+
```js
14+
// dependencies
15+
var fs = require("fs")
16+
var postcss = require("postcss")
17+
var colorHexAlpha = require("postcss-color-hex-alpha")
18+
19+
// css to be processed
20+
var css = fs.readFileSync("input.css", "utf8")
21+
22+
// process css
23+
var output = postcss()
24+
.use(colorHexAlpha())
25+
.process(css)
26+
.css
27+
```
28+
29+
Using this `input.css`:
30+
31+
```css
32+
body {
33+
background: #9d9c
34+
}
35+
36+
```
37+
38+
you will get:
39+
40+
```css
41+
body {
42+
background: rgba(153, 221, 153, 0.8)
43+
}
44+
```
45+
46+
Checkout [tests](test) for more examples.
47+
48+
---
49+
50+
## Contributing
51+
52+
Work on a branch, install dev-dependencies, respect coding style & run tests before submitting a bug fix or a feature.
53+
54+
$ git clone https://github.com/postcss/postcss-color-hex-alpha.git
55+
$ git checkout -b patch-1
56+
$ npm install
57+
$ npm test
58+
59+
## [Changelog](CHANGELOG.md)
60+
61+
## [License](LICENSE)
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/**
2+
* Module dependencies.
3+
*/
4+
var color = require("color")
5+
6+
/**
7+
* Constantes
8+
*/
9+
var HEX_ALPHA_RE = /#([0-9a-f]{4}(?:[0-9a-f]{4})?)\b/i
10+
var DECIMAL_PRECISION = 100000 // 5 decimals
11+
12+
/**
13+
* PostCSS plugin to transform hexa alpha colors
14+
*/
15+
module.exports = function plugin() {
16+
return function(style) {
17+
style.eachDecl(function transformDecl(dec) {
18+
if (!dec.value) {
19+
return
20+
}
21+
22+
dec.value = transform(dec.value, dec.source)
23+
})
24+
}
25+
}
26+
27+
/**
28+
* Transform colors to rgb() or rgba() on a declaration value
29+
*
30+
* @param {String} string
31+
* @return {String}
32+
*/
33+
function transform(string, source) {
34+
// order of transformation is important
35+
36+
try {
37+
if (string.indexOf("#") > -1) {
38+
string = transformHexAlpha(string, source)
39+
}
40+
}
41+
catch (e) {
42+
throw new Error(gnuMessage(e.message, source))
43+
}
44+
45+
return string
46+
}
47+
48+
/**
49+
* transform RGBA hexadecimal notations (#RRGGBBAA or #RGBA) to rgba().
50+
*
51+
* @param {String} string declaration value
52+
* @return {String} converted declaration value to rgba()
53+
*/
54+
function transformHexAlpha(string) {
55+
var m = HEX_ALPHA_RE.exec(string)
56+
if (!m) {
57+
return string
58+
}
59+
60+
var hex = m[1]
61+
62+
return string.slice(0, m.index) + hexaToRgba(hex) + transformHexAlpha(string.slice(m.index + 1 + hex.length))
63+
}
64+
65+
/**
66+
* transform RGBA or RRGGBBAA to rgba()
67+
*
68+
* @param {String} hex RGBA or RRGGBBAA
69+
* @return {String} converted value to rgba()
70+
*/
71+
function hexaToRgba(hex) {
72+
// if (hex.length === 3) {
73+
// hex += "f"
74+
// }
75+
if (hex.length === 4) {
76+
var h0 = hex.charAt(0)
77+
var h1 = hex.charAt(1)
78+
var h2 = hex.charAt(2)
79+
var h3 = hex.charAt(3)
80+
hex = h0 + h0 + h1 + h1 + h2 + h2 + h3 + h3
81+
}
82+
// if (hex.length === 6) {
83+
// hex += "ff"
84+
// }
85+
var rgb = []
86+
for (var i = 0, l = hex.length; i < l; i += 2) {
87+
rgb.push(Math.round(parseInt(hex.substr(i, 2), 16) / (i === 6 ? 255 : 1) * DECIMAL_PRECISION) / DECIMAL_PRECISION)
88+
}
89+
90+
return color({r: rgb[0], g: rgb[1], b: rgb[2], a: rgb[3]}).rgbaString()
91+
}
92+
93+
/**
94+
* return GNU style message
95+
*
96+
* @param {String} message
97+
* @param {Object} source
98+
*/
99+
function gnuMessage(message, source) {
100+
return (source ? (source.file ? source.file : "<css input>") + ":" + source.start.line + ":" + source.start.column : "") + " " + message
101+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"name": "postcss-color-hex-alpha",
3+
"version": "0.0.0",
4+
"description": "PostCSS plugin to transform W3C RGBA hexadecimal notations (#RRGGBBAA or #RGBA) to more compatible CSS (rgba())",
5+
"keywords": [
6+
"css",
7+
"postcss",
8+
"postcss-plugins",
9+
"color",
10+
"colour",
11+
"hexa",
12+
"alpha"
13+
],
14+
"author": "Maxime Thirouin",
15+
"license": "MIT",
16+
"repository": {
17+
"type": "git",
18+
"url": "https://github.com/postcss/postcss-color-hex-alpha.git"
19+
},
20+
"files": [
21+
"CHANGELOG.md",
22+
"LICENSE",
23+
"README.md",
24+
"index.js"
25+
],
26+
"dependencies": {
27+
"color": "^0.7.1"
28+
},
29+
"devDependencies": {
30+
"jscs": "^1.6.2",
31+
"jshint": "^2.5.6",
32+
"postcss": "^2.2.5",
33+
"tape": "^3.0.0"
34+
},
35+
"scripts": {
36+
"lint": "jscs *.js **/*.js && jshint . --exclude-path .gitignore",
37+
"test": "npm run lint && tape test"
38+
}
39+
}

0 commit comments

Comments
 (0)