Skip to content
This repository was archived by the owner on Jan 11, 2019. It is now read-only.

Commit a410f1d

Browse files
committed
Initial Commit
0 parents  commit a410f1d

File tree

9 files changed

+271
-0
lines changed

9 files changed

+271
-0
lines changed

.babelrc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"presets": [
3+
"es2015",
4+
"stage-1"
5+
],
6+
"ignore": [
7+
"/node_modules/"
8+
],
9+
"plugins": ["transform-runtime"]
10+
}

.editorconfig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# http://editorconfig.org
2+
root = true
3+
4+
[*]
5+
charset = utf-8
6+
indent_style = space
7+
indent_size = 2
8+
end_of_line = lf
9+
insert_final_newline = true
10+
trim_trailing_whitespace = true
11+
12+
[*.md]
13+
insert_final_newline = false
14+
trim_trailing_whitespace = false

.eslintrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "rackt"
3+
}

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Node
2+
node_modules
3+
*.log
4+
lib
5+
npm-debug.log
6+
7+
# Mac
8+
.DS_Store
9+
.*/

LICENSE.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
3+
The MIT License (MIT)
4+
5+
Copyright (c) 2015 Phil Plückthun
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
8+
9+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10+
11+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Goethe
2+
3+
## Installation
4+
5+
To install the latest version:
6+
7+
```
8+
npm install --save goethe
9+
```
10+
11+
## License
12+
13+
MIT

package.json

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"name": "goethe",
3+
"version": "0.1.0",
4+
"main": "lib/index.js",
5+
"jsnext:main": "src/index.js",
6+
"files": [
7+
"lib",
8+
"src"
9+
],
10+
"description": "Immutable color utility with magic conversion and manipulation.",
11+
"scripts": {
12+
"lint": "eslint src",
13+
"check": "npm run lint",
14+
"clean": "rm -rf lib",
15+
"build": "babel src --out-dir lib",
16+
"preversion": "npm run clean && npm run check",
17+
"version": "npm run build",
18+
"postversion": "git push && git push --tags"
19+
},
20+
"author": "Phil Plückthun <[email protected]> (https://github.com/philplckthun)",
21+
"bugs": {
22+
"url": "https://github.com/philplckthun/goethe/issues"
23+
},
24+
"license": "MIT",
25+
"keywords": [
26+
"goethe",
27+
"color",
28+
"colour",
29+
"css",
30+
"rgb",
31+
"hex"
32+
],
33+
"engines": {
34+
"node": ">= 0.12.0"
35+
},
36+
"devDependencies": {
37+
"babel-core": "^6.3.21",
38+
"babel-eslint": "^4.1.6",
39+
"babel-plugin-transform-decorators-legacy": "^1.3.4",
40+
"babel-plugin-transform-runtime": "^6.3.13",
41+
"babel-preset-es2015": "^6.3.13",
42+
"babel-preset-stage-1": "^6.3.13",
43+
"eslint": "^1.9.0"
44+
},
45+
"dependencies": {
46+
"color-string": "^0.3.0"
47+
}
48+
}

src/index.js

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
import {
2+
getRgba,
3+
hexString,
4+
rgbString,
5+
percentString,
6+
keyword,
7+
hslString
8+
} from 'color-string'
9+
import assert from './util/assert'
10+
11+
const WHITE = [ 255, 255, 255, 1 ]
12+
13+
function boundary(val, min = 0, max = 255) {
14+
return Math.min(max, Math.max(val, min))
15+
}
16+
17+
const proto = {
18+
// Manually set RGBA
19+
red(val) {
20+
const x = this.data
21+
return createColor([
22+
boundary(val), x[1], x[2], x[3]
23+
])
24+
},
25+
green(val) {
26+
const x = this.data
27+
return createColor([
28+
x[0], boundary(val), x[2], x[3]
29+
])
30+
},
31+
blue(val) {
32+
const x = this.data
33+
return createColor([
34+
x[0], x[1], boundary(val), x[3]
35+
])
36+
},
37+
opacity(val) {
38+
const x = this.data
39+
return createColor([
40+
x[0], x[1], x[2], boundary(val, 0, 1)
41+
])
42+
},
43+
44+
// Factor opacity
45+
clearer(factor) {
46+
const [ r, g, b, a ] = this.data
47+
return createColor([
48+
r, g, b, boundary(a * factor, 0, 1)
49+
])
50+
},
51+
opaquer(factor) {
52+
const [ r, g, b, a ] = this.data
53+
return createColor([
54+
r, g, b, boundary(a / factor, 0, 1)
55+
])
56+
},
57+
58+
// Factor RGB
59+
darken(factor) {
60+
const [ r, g, b, a ] = this.data
61+
return createColor([
62+
...[ r, g, b ].map(x => boundary(x * (1 - factor))),
63+
a
64+
])
65+
},
66+
lighten(factor) {
67+
const [ r, g, b, a ] = this.data
68+
return createColor([
69+
...[ r, g, b ].map(x => boundary(x + (255 - x) * (1 - factor))),
70+
a
71+
])
72+
},
73+
74+
// Raw return values
75+
getRaw() {
76+
return this.data.slice()
77+
},
78+
raw() {
79+
return this.data.slice()
80+
},
81+
82+
// Convert to object
83+
toRGB() {
84+
const x = this.data
85+
return { r: x[0], g: x[1], b: x[2], a: x[3] }
86+
},
87+
toRGBA() {
88+
return this.toRGB()
89+
},
90+
toObject() {
91+
return this.toRGB()
92+
},
93+
94+
// Convert to String
95+
toString(mode) {
96+
if (mode === 'hex') {
97+
return hexString(this.data)
98+
} else if (mode === 'percent') {
99+
return percentString(this.data)
100+
} else if (mode === 'keyword') {
101+
return keyword(this.data)
102+
} else if (mode === 'hsl') {
103+
return hslString(this.data)
104+
}
105+
106+
return rgbString(this.data)
107+
},
108+
109+
// Compatibility
110+
hexString() {
111+
return this.toString('hex')
112+
},
113+
rgbString() {
114+
return this.toString()
115+
},
116+
117+
// Automatically display a hex string
118+
valueOf() {
119+
return this.toString()
120+
}
121+
}
122+
123+
function isRGB(x) {
124+
return typeof x === 'number' && x >= 0 && x < 256
125+
}
126+
127+
function isRGBArray(arr) {
128+
return arr.reduce(
129+
(state = true, x) => state && isRGB(x)
130+
)
131+
}
132+
133+
function createColor(tupel) {
134+
const res = Object.create(proto)
135+
res.data = tupel || WHITE.slice()
136+
return res
137+
}
138+
139+
export default function Color(init) {
140+
if (typeof init === 'string') {
141+
return createColor(getRgba(init))
142+
} else if (Array.isArray(init)) {
143+
assert((
144+
init.length === 3 || init.length === 4
145+
) && isRGBArray(init), 'Expected array to only contain RGB numbers.')
146+
147+
if (init.length === 3) {
148+
return createColor([ ...init, 1 ])
149+
}
150+
return createColor(init.slice())
151+
} else if (typeof init === 'object') {
152+
assert(
153+
isRGB(init.r) && isRGB(init.g) && isRGB(init.b),
154+
'Expected object to contain RGB properties.'
155+
)
156+
return createColor([ init.r, init.g, init.b, init.a || 1 ])
157+
}
158+
159+
return createColor()
160+
}

src/util/assert.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function assert(invariant, msg) {
2+
if (!invariant) throw msg
3+
}

0 commit comments

Comments
 (0)