Skip to content
This repository was archived by the owner on Apr 3, 2023. It is now read-only.

Commit 85a26ae

Browse files
committed
Library core functionality
1 parent dbcfd09 commit 85a26ae

7 files changed

+499
-0
lines changed

.editorconfig

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
root = true
2+
3+
[*]
4+
indent_style = tab
5+
end_of_line = lf
6+
charset = utf-8
7+
trim_trailing_whitespace = true
8+
insert_final_newline = true
9+
10+
[*.{json,js,jsx,html,css}]
11+
indent_style = space
12+
indent_size = 2
13+
14+
[.eslintrc]
15+
indent_style = space
16+
indent_size = 2
17+
18+
[*.md]
19+
trim_trailing_whitespace = false

.eslintrc

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"parser": "babel-eslint",
3+
"extends": "airbnb",
4+
"env": {
5+
"browser": true,
6+
"mocha": true,
7+
"node": true
8+
},
9+
"rules": {
10+
"consistent-return": 0,
11+
"comma-dangle": 0,
12+
"no-use-before-define": 0,
13+
"react/jsx-no-bind": 0,
14+
"react/prefer-stateless-function": 0
15+
},
16+
# "plugins": [
17+
# "import"
18+
# ],
19+
"settings": {
20+
"import/resolver": "node"
21+
}
22+
}

.gitignore

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
2+
# Created by https://www.gitignore.io/api/node,macos
3+
4+
### Node ###
5+
# Logs
6+
logs
7+
*.log
8+
npm-debug.log*
9+
10+
# Runtime data
11+
pids
12+
*.pid
13+
*.seed
14+
*.pid.lock
15+
16+
# Directory for instrumented libs generated by jscoverage/JSCover
17+
lib-cov
18+
19+
# Coverage directory used by tools like istanbul
20+
coverage
21+
22+
# nyc test coverage
23+
.nyc_output
24+
25+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
26+
.grunt
27+
28+
# node-waf configuration
29+
.lock-wscript
30+
31+
# Compiled binary addons (http://nodejs.org/api/addons.html)
32+
build/Release
33+
34+
# Dependency directories
35+
node_modules
36+
jspm_packages
37+
38+
# Optional npm cache directory
39+
.npm
40+
41+
# Optional eslint cache
42+
.eslintcache
43+
44+
# Optional REPL history
45+
.node_repl_history
46+
47+
# Output of 'npm pack'
48+
*.tgz
49+
50+
51+
### macOS ###
52+
*.DS_Store
53+
.AppleDouble
54+
.LSOverride
55+
56+
# Icon must end with two \r
57+
Icon
58+
# Thumbnails
59+
._*
60+
# Files that might appear in the root of a volume
61+
.DocumentRevisions-V100
62+
.fseventsd
63+
.Spotlight-V100
64+
.TemporaryItems
65+
.Trashes
66+
.VolumeIcon.icns
67+
.com.apple.timemachine.donotpresent
68+
# Directories potentially created on remote AFP share
69+
.AppleDB
70+
.AppleDesktop
71+
Network Trash Folder
72+
Temporary Items
73+
.apdisk
74+
75+
# Custom
76+
lib

README

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
> Material takes cues from contemporary architecture, road signs, pavement marking tape, and athletic courts. Color should be **unexpected** and **vibrant**. ([https://material.google.com/style/color.html#](https://material.google.com/style/color.html#))
2+
3+
# Material color hashing
4+
5+
Hash a string to a Material Design color
6+
7+
A dead-simple library to hash a string to a {background-color, text-color} tuple from the official Material Design palette.
8+
9+
Great for dinamically coloring your UI elements (badges, list icons, ..) with good-looking, vibrant colors and ensured text legibility.
10+
11+
## Usage
12+
13+
```bash
14+
$ npm install material-color-hash
15+
```

package.json

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "material-color-hash",
3+
"version": "0.0.0",
4+
"description": "Hash strings to Material UI colors",
5+
"main": "./lib/index.js",
6+
"scripts": {
7+
"clean": "rm -rf lib",
8+
"build": "babel src --out-dir lib",
9+
"prepublish": "NODE_ENV=production; babel src --out-dir lib"
10+
},
11+
"keywords": [
12+
"color",
13+
"hash",
14+
"material",
15+
"ui"
16+
],
17+
"author": "Belka <[email protected]> (http://belka.us)",
18+
"license": "MIT",
19+
"devDependencies": {
20+
"babel-eslint": "^7.0.0",
21+
"babel-preset-es2015": "^6.16.0",
22+
"eslint": "^3.8.1",
23+
"eslint-config-airbnb": "^12.0.0",
24+
"eslint-plugin-import": "^1.16.0",
25+
"eslint-plugin-jsx-a11y": "^2.2.2",
26+
"eslint-plugin-react": "^6.3.0"
27+
},
28+
"dependencies": {
29+
"string-hash": "^1.1.0"
30+
}
31+
}

src/index.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import hash from 'string-hash';
2+
import materialColors from './materialColors';
3+
4+
const availableShades = ['50', '100', '200', '300', '400', '500', '600', '700', '800', '900'];
5+
const materialWhite = 'rgba(255, 255, 255, 1)';
6+
const materialBlack = 'rgba(0, 0, 0, 0.87)';
7+
8+
/* Get the whole color object, including all the shades, name and white breakpoint */
9+
function getColorObj(text) {
10+
const hashedText = hash(text);
11+
const colorIndex = hashedText % materialColors.length;
12+
13+
return materialColors[colorIndex];
14+
}
15+
16+
/* Get a ready-to-use Style object of the Material color of a string */
17+
export default function toMaterialStyle(text, shade) {
18+
let shadeStr = `${shade}`; // convert shade to string
19+
if (!availableShades.includes(shadeStr)) {
20+
shadeStr = '500';
21+
}
22+
23+
const colorObj = getColorObj(text);
24+
25+
return {
26+
backgroundColor: colorObj.shades[shadeStr],
27+
color: parseInt(shadeStr, 10) >= colorObj.whiteBreakpoint ? materialWhite : materialBlack,
28+
materiaColorName: colorObj.name
29+
};
30+
}

0 commit comments

Comments
 (0)