Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

converted the class based component to functional component #14

Merged
merged 3 commits into from
Oct 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import './App.css';
import '../node_modules/bootstrap/dist/css/bootstrap.min.css';
import ColorPickerComponent from './ColorPickerComponent'

function App() {
return (
<div className="App">
<ColorPickerComponent />
</div>
);

const App = () => {
return (
<div className="App">
<ColorPickerComponent />
</div>
)
}

export default App;
117 changes: 62 additions & 55 deletions src/ColorGrid.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,67 @@
import React from 'react'
import reactCSS from 'reactcss'
import React from 'react';
import reactCSS from 'reactcss';
import './App.css';

class ColorGrid extends React.Component {
const ColorGrid = ({ hueValue, saturationCount, luminosityCount }) => {
const { outerWidth: width, outerHeight: height } = window;

render() {
const { h, satCount, lumCount } = this.props;
const { outerWidth: width, outerHeight: height } = window;
const sFraction = ( 100 / ( satCount - 2 ) );
const lFraction = ( 100 / ( lumCount - 2 ) );
const oneColorWidth = width / ( satCount - 2 ) / 2;
const oneColorHeight = height / ( lumCount - 2 ) / 2;
// console.log( h, a, satCount, lumCount );
console.log( oneColorWidth, oneColorHeight );
return (
<div className="colorgrid" style={{ border: "1px", borderBlock: "10px", borderColor: 'red' }}>
{/* create satCount divs */}
{Array( satCount - 1 ).fill( 0 ).map( ( _, i ) => {
return (
<div key={i} style={{ display: 'flex', flexDirection: 'row' }}>
{/* create lumCount divs */}
{Array( lumCount - 1 ).fill( 0 ).map( ( _, j ) => {
var hLocal = h;
var sLocal = i * sFraction;
var lLocal = j * lFraction;
const styles = reactCSS( {
'default': {
color: {
width: `${oneColorWidth}px`,
height: `${oneColorHeight}px`,
borderRadius: '5px',
background: `hsl(${hLocal}, ${sLocal}%, ${lLocal}%)`,
},
swatch: {
padding: '1px',
background: '#fff',
borderRadius: '5px',
boxShadow: '0 0 0 1px rgba(0,0,0,.1)',
display: 'inline-block',
cursor: 'pointer',
},
},
} );
return (
<div key={j} style={styles.swatch}>
<div style={styles.color} />
</div>
)
} )}
</div>
)
} )}
</div>
const saturationFraction = 100 / (saturationCount - 2);
const luminosityFraction = 100 / (luminosityCount - 2);

const oneColorWidth = width / (saturationCount - 2) / 2;
const oneColorHeight = height / (luminosityCount - 2) / 2;

)
}
}
return (
<div
className='colorgrid'
style={{ border: '1px', borderBlock: '10px', borderColor: 'red' }}
>
{
// create saturationCount divs
Array(saturationCount - 1)
.fill(0)
.map((_, i) => {
return (
<div key={i} style={{ display: 'flex', flexDirection: 'row' }}>
{
// create luminosityCount divs
Array(luminosityCount - 1)
.fill(0)
.map((_, j) => {
var hueLocal = hueValue;
var saturationLocal = j * saturationFraction;
var luminosityLocal = j * luminosityFraction;
const styles = reactCSS({
default: {
color: {
width: `${oneColorWidth}px`,
height: `${oneColorHeight}px`,
borderRadius: '5px',
background: `hsl(${hueLocal}, ${saturationLocal}%, ${luminosityLocal}%)`,
},
swatch: {
padding: '1px',
background: '#fff',
borderRadius: '5px',
boxShadow: '0 0 0 1px rgba(0,0,0,.1)',
display: 'inline-block',
cursor: 'pointer',
},
},
});
return (
<div key={j} style={styles.swatch}>
<div style={styles.color} />
</div>
);
})
}
</div>
);
})
}
</div>
);
};

export default ColorGrid
export default ColorGrid;
81 changes: 35 additions & 46 deletions src/ColorPickerComponent.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,35 @@
import React from 'react'
import { SketchPicker } from 'react-color'
import { rgb, hsl } from 'color-convert'
import ColorGrid from './ColorGrid'

class ColorPickerComponent extends React.Component {
state = {
displayColorPicker: false,
color: {
r: 241,
g: 112,
b: 19,
},
};

handleClick = () => {
this.setState( { displayColorPicker: !this.state.displayColorPicker } )
};

handleClose = () => {
this.setState( { displayColorPicker: false } )
};

handleChange = ( color ) => {
this.setState( { color: color.rgb } )
};
render() {
var colorAsHSL = rgb.hsl(this.state.color.r, this.state.color.g, this.state.color.b);
return (
<div className='main'>
<div >
<ColorGrid
h={colorAsHSL[0]}
satCount="15"
lumCount="15"
/>
</div>
<div className='colorpick'>
<SketchPicker color={this.state.color} onChange={this.handleChange} />
</div>
</div>
)
}
}

export default ColorPickerComponent
import React, { useState } from 'react';
import { SketchPicker } from 'react-color';
import { rgb } from 'color-convert';
import ColorGrid from './ColorGrid';


const ColorPickerComponent = () => {
const [displayColorPicker, setDisplayColorPicker] = useState(false);
const [color, setColor] = useState({ r: 241, g: 112, b: 19 });

const handleClick = () => setDisplayColorPicker((prev) => !prev);

const handleClose = () => setDisplayColorPicker(false);

const handleChange = (color) => setColor(color.rgb);

const colorAsHSL = rgb.hsl(color.r, color.g, color.b);

return (
<div className='main'>
<div>
<ColorGrid
hueValue={colorAsHSL[0]}
saturationCount='15'
luminosityCount='15'
/>
</div>
<div className='colorpick'>
<SketchPicker color={color} onChange={handleChange} />
</div>
</div>
);
};

export default ColorPickerComponent;