diff --git a/src/App.js b/src/App.js index 9b70163..a311850 100644 --- a/src/App.js +++ b/src/App.js @@ -1,9 +1,8 @@ +import chroma from 'chroma-js'; import React from 'react'; import styled from 'styled-components'; import './App.css'; -const DEFAULT_SHADE_COUNT = 9; - // an object to hold our styled components const S = {}; @@ -23,22 +22,53 @@ S.Shade = styled(Shade)` class Color extends React.Component { constructor(props) { super(props); + const tints = []; + const shades = []; this.state = { baseColor: this.props.baseColor, - shades: [], - shadeCount: this.props.shadeCount ? this.props.shadeCount : DEFAULT_SHADE_COUNT, - } - for (let index = 0; index < this.state.shadeCount; index++) { - this.state.shades.push(this.props.baseColor); + chromaColor: chroma(this.props.baseColor), + tints: tints, + shades: shades, } } + setTints(brightenAmount, start, end, step) { + this.setState((state) => { + const scaleTints = chroma.scale([state.chromaColor.brighten(brightenAmount), state.chromaColor]); + const tints = []; + for (let i = start; i < end; i += step) { + tints.push(scaleTints(i).hex()); + } + return {tints: tints} + }); + } + + setShades(darkenAmount, start, end, step) { + this.setState((state) => { + const scaleShades = chroma.scale([state.chromaColor, state.chromaColor.darken(darkenAmount)]); + const shades = []; + for (let i = start; i < end; i += step) { + shades.push(scaleShades(i).hex()); + } + return {shades: shades} + }); + } + + componentDidMount() { + this.setTints(2, 0.2, 0.9, 0.2); + this.setShades(5, 0.1, 0.9, 0.1); + } + render() { return ( ); } @@ -53,7 +83,7 @@ margin: 0; function App() { return (
- +
); }