|
1 | | -const React = require('react') |
2 | | -const tinycolor = require('tinycolor2') |
| 1 | +const React = require('react'); |
| 2 | +const tinycolor = require('tinycolor2'); |
3 | 3 |
|
4 | | -const _ = require('lodash') |
5 | | -const Dropzone = require('react-dropzone') |
| 4 | +const _ = require('lodash'); |
| 5 | +const Dropzone = require('react-dropzone'); |
6 | 6 |
|
7 | | -const {base64ImageToRGBMatrix} = require('base64-image-utils') |
| 7 | +const { base64ImageToRGBMatrix } = require('base64-image-utils'); |
8 | 8 |
|
9 | | -function compressColor (rgb) { |
10 | | - const hex = tinycolor(rgb).toHexString() |
| 9 | +function compressColor(rgb) { |
| 10 | + const hex = tinycolor(rgb).toHexString(); |
11 | 11 |
|
12 | 12 | switch (hex) { // based on CSS3 supported color names http://www.w3.org/TR/css3-color/ |
13 | | - case '#c0c0c0': return 'silver' |
14 | | - case '#808080': return 'gray' |
15 | | - case '#800000': return 'maroon' |
16 | | - case '#ff0000': return 'red' |
17 | | - case '#800080': return 'purple' |
18 | | - case '#008000': return 'green' |
19 | | - case '#808000': return 'olive' |
20 | | - case '#000080': return 'navy' |
21 | | - case '#008080': return 'teal' |
| 13 | + case '#c0c0c0': |
| 14 | + return 'silver'; |
| 15 | + case '#808080': |
| 16 | + return 'gray'; |
| 17 | + case '#800000': |
| 18 | + return 'maroon'; |
| 19 | + case '#ff0000': |
| 20 | + return 'red'; |
| 21 | + case '#800080': |
| 22 | + return 'purple'; |
| 23 | + case '#008000': |
| 24 | + return 'green'; |
| 25 | + case '#808000': |
| 26 | + return 'olive'; |
| 27 | + case '#000080': |
| 28 | + return 'navy'; |
| 29 | + case '#008080': |
| 30 | + return 'teal'; |
22 | 31 | } |
23 | | - return hex[1] === hex[2] && hex[3] === hex[4] && hex[5] === hex[6] ? '#' + hex[1] + hex[3] + hex[5] : hex |
| 32 | + return hex[1] === hex[2] && hex[3] === hex[4] && hex[5] === hex[6] ? '#' + hex[1] + hex[3] + hex[5] : hex; |
24 | 33 | } |
25 | 34 |
|
26 | 35 | var App = React.createClass({ |
27 | | - |
28 | | - getInitialState () { |
| 36 | + getInitialState() { |
29 | 37 | return { |
30 | 38 | rgbArray: null, |
31 | 39 | loadingImage: false |
32 | | - } |
| 40 | + }; |
33 | 41 | }, |
34 | 42 |
|
35 | | - onDrop (files) { |
36 | | - var file = files[0] |
37 | | - var fr = new window.FileReader() |
| 43 | + onDrop(files) { |
| 44 | + var file = files[0]; |
| 45 | + var fr = new window.FileReader(); |
38 | 46 |
|
39 | 47 | if (window.ga) { |
40 | | - window.ga('send', 'event', 'button', 'click', 'img2css execute') |
| 48 | + window.ga('send', 'event', 'button', 'click', 'img2css execute'); |
41 | 49 | } |
42 | 50 |
|
43 | 51 | this.setState({ |
44 | 52 | loadingImage: true |
45 | | - }) |
| 53 | + }); |
46 | 54 |
|
47 | | - fr.onload = (data) => { |
48 | | - const base64 = data.currentTarget.result |
| 55 | + fr.onload = data => { |
| 56 | + const base64 = data.currentTarget.result; |
49 | 57 |
|
50 | 58 | if (base64.length > 100000) { |
51 | | - let confirmation = window.confirm('Your image is really big, do you really want to try to convert it?') |
| 59 | + let confirmation = window.confirm('Your image is really big, do you really want to try to convert it?'); |
52 | 60 |
|
53 | 61 | if (!confirmation) { |
54 | 62 | this.setState({ |
55 | 63 | loadingImage: false |
56 | | - }) |
57 | | - return |
| 64 | + }); |
| 65 | + return; |
58 | 66 | } |
59 | 67 | } |
60 | 68 |
|
61 | 69 | base64ImageToRGBMatrix(base64, (err, data) => { |
62 | | - if (err) return console.error(err) |
| 70 | + if (err) return console.error(err); |
63 | 71 |
|
64 | 72 | this.setState({ |
65 | 73 | rgbMatrix: data, |
66 | 74 | loadingImage: false |
67 | | - }) |
68 | | - }) |
69 | | - } |
70 | | - fr.readAsDataURL(file) |
| 75 | + }); |
| 76 | + }); |
| 77 | + }; |
| 78 | + fr.readAsDataURL(file); |
71 | 79 | }, |
72 | 80 |
|
73 | | - render () { |
74 | | - var {rgbMatrix, loadingImage} = this.state |
| 81 | + render() { |
| 82 | + var { rgbMatrix, loadingImage } = this.state; |
75 | 83 |
|
76 | 84 | var masterShadow = _.map(rgbMatrix, (row, rowIndex) => { |
77 | 85 | return _.map(row, (col, colIndex) => { |
78 | | - var color = compressColor(`rgb(${col.r},${col.g},${col.b})`) |
| 86 | + var color = compressColor(`rgb(${col.r},${col.g},${col.b})`); |
79 | 87 |
|
80 | | - return `${color} ${colIndex ? colIndex + 'px' : 0} ${rowIndex ? rowIndex + 'px' : 0}` |
81 | | - }).join(',') |
82 | | - }).join(',') |
| 88 | + return `${color} ${colIndex ? colIndex + 'px' : 0} ${rowIndex ? rowIndex + 'px' : 0}`; |
| 89 | + }).join(','); |
| 90 | + }).join(','); |
83 | 91 |
|
84 | 92 | return ( |
85 | | - <div className='padding-horizontal-2x'> |
| 93 | + <div className="padding-horizontal-2x"> |
86 | 94 |
|
87 | | - <Dropzone onDrop={this.onDrop} className='dropZone'> |
| 95 | + <Dropzone onDrop={this.onDrop} className="dropZone"> |
88 | 96 | {loadingImage ? 'Processing...' : 'Drop an image here, or click to upload.'} |
89 | 97 | </Dropzone> |
90 | 98 |
|
91 | | - {rgbMatrix && ( |
| 99 | + {rgbMatrix && |
92 | 100 | <div> |
93 | | - <div className='tutorial'> |
| 101 | + <div className="tutorial"> |
94 | 102 | This is your pure css (and single div) image! Enjoy! {masterShadow.length.toLocaleString()}b |
95 | 103 | </div> |
96 | 104 |
|
97 | | - <div className='pixel' style={{ |
98 | | - height: 1, |
99 | | - width: 1, |
100 | | - boxShadow: masterShadow, |
101 | | - marginBottom: rgbMatrix.length |
102 | | - }} /> |
103 | | - |
104 | | - <div className='tutorial'> |
| 105 | + <div |
| 106 | + className="pixel" |
| 107 | + style={{ |
| 108 | + height: 1, |
| 109 | + width: 1, |
| 110 | + boxShadow: masterShadow, |
| 111 | + marginBottom: rgbMatrix.length |
| 112 | + }} |
| 113 | + /> |
| 114 | + |
| 115 | + <div className="tutorial"> |
105 | 116 | The code: |
106 | 117 | </div> |
107 | 118 |
|
108 | | - <div className='code'> |
| 119 | + <div className="code"> |
109 | 120 | {`<div style="height: 1px; width: 1px; box-shadow: ${masterShadow}"></div>`} |
110 | 121 | </div> |
111 | | - </div> |
112 | | - )} |
| 122 | + </div>} |
113 | 123 |
|
114 | 124 | </div> |
115 | | - ) |
| 125 | + ); |
116 | 126 | } |
117 | | -}) |
| 127 | +}); |
118 | 128 |
|
119 | | -module.exports = App |
| 129 | +module.exports = App; |
0 commit comments