Skip to content

Commit 2e19690

Browse files
authored
Merge pull request #12 from howdyAnkit/RandomCOlor_Picker
RandomColor_Picker Updated
2 parents 3cb84d6 + c94db5d commit 2e19690

File tree

4 files changed

+155
-0
lines changed

4 files changed

+155
-0
lines changed

RandomColor_Picker/Button.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import React from 'react';
2+
3+
export class Button extends React.Component {
4+
render() {
5+
return (
6+
<button
7+
className={ this.props.light ? 'light-button' : 'dark-button' }
8+
onClick={this.props.onClick}>
9+
Refresh
10+
</button>
11+
);
12+
}
13+
}

RandomColor_Picker/Random.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom';
3+
import {Button} from './Button';
4+
5+
class Random extends React.Component {
6+
constructor(props){
7+
super(props);
8+
this.state = {
9+
color:[255,0,0]
10+
};
11+
this.handleClick = this.handleClick.bind(this);
12+
}
13+
14+
componentDidMount() {
15+
this.applyColor();
16+
}
17+
18+
componentDidUpdate(prevProps, prevState) {
19+
this.applyColor();
20+
}
21+
22+
handleClick() {
23+
this.setState({
24+
color: this.chooseColor()
25+
})
26+
}
27+
28+
formatColor(ary) {
29+
return 'rgb(' + ary.join(', ') + ')';
30+
}
31+
32+
isLight() {
33+
const rgb = this.state.color;
34+
return rgb.reduce((a,b) => a+b) < 127 * 3;
35+
}
36+
37+
applyColor() {
38+
const color = this.formatColor(this.state.color);
39+
document.body.style.background = color;
40+
}
41+
42+
chooseColor() {
43+
const random = [];
44+
for (let i = 0; i < 3; i++) {
45+
random.push(Math.floor(Math.random()*256));
46+
}
47+
return random;
48+
}
49+
50+
render() {
51+
return (
52+
<div>
53+
<h1 className={this.isLight() ? 'white' : 'black'}>
54+
Your color is {this.formatColor(this.state.color)};
55+
</h1>
56+
<Button light={this.isLight()} onClick={this.handleClick} />
57+
</div>
58+
);
59+
}
60+
}
61+
62+
ReactDOM.render(
63+
<Random />,
64+
document.getElementById('app')
65+
);

RandomColor_Picker/index.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<link rel="stylesheet" href="./styles.css">
6+
<title>Random Color</title>
7+
</head>
8+
<body>
9+
<div id="app"></div>
10+
<script src="https://content.codecademy.com/courses/React/react-course-bundle.min.js"></script>
11+
<script src="/Random.compiled.js"></script>
12+
</body>
13+
</html>

RandomColor_Picker/style.css

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
html, body {
2+
margin: 0;
3+
height: 100%;
4+
}
5+
6+
body {
7+
background-color: #ffffff;
8+
font-family: Helvetica, Arial, sans-serif;
9+
text-align: center;
10+
}
11+
12+
#app {
13+
position: relative;
14+
height: 100%;
15+
width: 100%;
16+
padding-top: 10px;
17+
}
18+
19+
#app div {
20+
width: 100%;
21+
}
22+
23+
#app div div {
24+
height: 100%;
25+
}
26+
27+
#app div div div {
28+
position: relative;
29+
height: auto;
30+
}
31+
32+
h1, h2 {
33+
margin-left: 5%;
34+
margin-right: 5%;
35+
}
36+
37+
button {
38+
border-radius: 8px;
39+
padding: 15px 32px;
40+
text-align: center;
41+
text-decoration: none;
42+
display: inline-block;
43+
font-size: 16px;
44+
font-family: 'Oxygen', sans-serif;
45+
letter-spacing: 2px;
46+
}
47+
48+
.black {
49+
color: black;
50+
}
51+
52+
.white {
53+
color: white;
54+
}
55+
56+
.light-button {
57+
background-color: rgba(255,255,255,0.5);
58+
color: rgb(0,0,0);
59+
}
60+
61+
.dark-button {
62+
background-color: rgba(0,0,0,0.5);
63+
color: rgb(255,255,255);
64+
}

0 commit comments

Comments
 (0)