Skip to content

Commit 4316e26

Browse files
committed
Initial release from postcss-color
Ref postcss/postcss-color#4
0 parents  commit 4316e26

13 files changed

+376
-0
lines changed

.editorconfig

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

.gitignore

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

.jscsrc

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+
}

.jshintrc

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+
}

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
language: node_js

CHANGELOG.md

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)

LICENSE

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.

README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# postcss-color-hwb [![Build Status](https://travis-ci.org/postcss/postcss-color-hwb.png)](https://travis-ci.org/postcss/postcss-color-hwb)
2+
3+
> [PostCSS](https://github.com/postcss/postcss) plugin to transform [W3C CSS hwb() color](http://dev.w3.org/csswg/css-color/#the-hwb-notation) to more compatible CSS (rgb() (or rgba())).
4+
5+
## Installation
6+
7+
```bash
8+
$ npm install postcss-color-hwb
9+
```
10+
11+
## Usage
12+
13+
```js
14+
// dependencies
15+
var fs = require("fs")
16+
var postcss = require("postcss")
17+
var colorHwb = require("postcss-color-hwb")
18+
19+
// css to be processed
20+
var css = fs.readFileSync("input.css", "utf8")
21+
22+
// process css
23+
var output = postcss()
24+
.use(colorHwb())
25+
.process(css)
26+
.css
27+
```
28+
29+
Using this `input.css`:
30+
31+
```css
32+
body {
33+
color: hwb(90, 0%, 0%, 0.5);
34+
}
35+
36+
```
37+
38+
you will get:
39+
40+
```css
41+
body {
42+
color: rgba(128, 255, 0, 0.5);
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-hwb.git
55+
$ git checkout -b patch-1
56+
$ npm install
57+
$ npm test
58+
59+
## [Changelog](CHANGELOG.md)
60+
61+
## [License](LICENSE)

index.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* Module dependencies.
3+
*/
4+
var color = require("color")
5+
var reduceFunctionCall = require("reduce-function-call")
6+
7+
/**
8+
* PostCSS plugin to transform hwb() to rgb()
9+
*/
10+
module.exports = function plugin() {
11+
return function(style) {
12+
style.eachDecl(function transformDecl(dec) {
13+
if (!dec.value) {
14+
return
15+
}
16+
17+
dec.value = transform(dec.value, dec.source)
18+
})
19+
}
20+
}
21+
22+
/**
23+
* Transform hwb color to rgb() or rgba()
24+
*
25+
* @param {String} string
26+
* @return {String}
27+
*/
28+
function transform(string, source) {
29+
try {
30+
if (string.indexOf("hwb(") > -1) {
31+
string = transformHwb(string, source)
32+
}
33+
}
34+
catch (e) {
35+
throw new Error(gnuMessage(e.message, source))
36+
}
37+
38+
return string
39+
}
40+
41+
42+
/**
43+
* transform hwb() to rgb() (or rgba())
44+
*
45+
* @param {String} string declaration value
46+
* @return {String} converted declaration value to rgba()
47+
*/
48+
function transformHwb(string) {
49+
return reduceFunctionCall(string, "hwb", function(body, fn) {
50+
return color(fn + "(" + body + ")").rgbString()
51+
})
52+
}
53+
54+
/**
55+
* return GNU style message
56+
*
57+
* @param {String} message
58+
* @param {Object} source
59+
*/
60+
function gnuMessage(message, source) {
61+
return (source ? (source.file ? source.file : "<css input>") + ":" + source.start.line + ":" + source.start.column : "") + " " + message
62+
}

package.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"name": "postcss-color-hwb",
3+
"version": "0.0.0",
4+
"description": "PostCSS plugin to transform W3C CSS hwb() color to more compatible CSS (rgb() (or rgba()))",
5+
"keywords": [
6+
"css",
7+
"postcss",
8+
"postcss-plugins",
9+
"color",
10+
"colour",
11+
"rgb",
12+
"hwb"
13+
],
14+
"author": "Maxime Thirouin",
15+
"license": "MIT",
16+
"repository": {
17+
"type": "git",
18+
"url": "https://github.com/postcss/postcss-color-hwb.git"
19+
},
20+
"files": [
21+
"CHANGELOG.md",
22+
"LICENSE",
23+
"README.md",
24+
"index.js"
25+
],
26+
"dependencies": {
27+
"color": "^0.7.1",
28+
"reduce-function-call": "^1.0.1"
29+
},
30+
"devDependencies": {
31+
"jscs": "^1.6.2",
32+
"jshint": "^2.5.6",
33+
"postcss": "^2.2.5",
34+
"tape": "^3.0.0"
35+
},
36+
"scripts": {
37+
"lint": "jscs *.js **/*.js && jshint . --exclude-path .gitignore",
38+
"test": "npm run lint && tape test"
39+
}
40+
}

test/fixtures/hwb.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
body {
2+
color: hwb(90, 0%, 0%, 0.5);
3+
background: linear-gradient(hwb(190, 50%, 0%), hwb(190, 50%, 10%, .2));
4+
}

test/fixtures/hwb.expected.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
body {
2+
color: rgba(128, 255, 0, 0.5);
3+
background: linear-gradient(rgb(128, 234, 255), rgba(128, 212, 230, 0.2));
4+
}

test/index.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
var fs = require("fs")
2+
3+
var test = require("tape")
4+
5+
var postcss = require("postcss")
6+
var plugin = require("..")
7+
8+
function filename(name) { return "test/" + name + ".css" }
9+
function read(name) { return fs.readFileSync(name, "utf8") }
10+
11+
function compareFixtures(t, name, msg, opts, postcssOpts) {
12+
postcssOpts = postcssOpts || {}
13+
postcssOpts.from = filename("fixtures/" + name)
14+
opts = opts || {}
15+
var actual = postcss().use(plugin(opts)).process(read(postcssOpts.from), postcssOpts).css
16+
var expected = read(filename("fixtures/" + name + ".expected"))
17+
fs.writeFile(filename("fixtures/" + name + ".actual"), actual)
18+
t.equal(actual, expected, msg)
19+
}
20+
21+
test("hwb", function(t) {
22+
compareFixtures(t, "hwb", "should transform hwb")
23+
t.end()
24+
})

0 commit comments

Comments
 (0)