Skip to content

Commit

Permalink
- Add color picker
Browse files Browse the repository at this point in the history
  • Loading branch information
Hoocs151 committed May 10, 2023
1 parent 95ffd5e commit a5ac893
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
6 changes: 6 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@

<body>
<div class="container">
<div class="color-picker-wrapper">
<label for="color-picker">
Color Picker:
</label>
<input type="color" id="color-picker" value="#000000" />
</div>
<div class="wrapper">
<label for="red">
R
Expand Down
19 changes: 19 additions & 0 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,25 @@ const rColor = document.getElementById("red")
const gColor = document.getElementById("green")
const bColor = document.getElementById("blue")

const colorPicker = document.querySelector("#color-picker")

const pickColor = () => {
const hex = colorPicker.value
const rgb = hexToRgb(hex)
rColor.value = rgb.r
gColor.value = rgb.g
bColor.value = rgb.b
generateColor()
}

const hexToRgb = (hex) => {
const r = parseInt(hex.substring(1,3), 16)
const g = parseInt(hex.substring(3,5), 16)
const b = parseInt(hex.substring(5,7), 16)
return { r, g, b }
}

colorPicker.addEventListener("change", pickColor)

const generateColor = () => {
let rColorValue = rColor.value
Expand Down
25 changes: 25 additions & 0 deletions style.css
Original file line number Diff line number Diff line change
Expand Up @@ -257,4 +257,29 @@ button:focus {
#copy-btn:focus {
outline: none;
box-shadow: 0 0 0 2px #0075ff;
}

.color-picker-wrapper {
display: flex;
align-items: center;
gap: 1em;
margin-bottom: 2.2em;
}

.color-picker-wrapper label {
font-size: 1.2em;
color: #6c757d;
margin-right: 0.5em;
}

.color-picker-wrapper input[type="color"] {
width: 40px;
height: 40px;
padding: 0;
border: none;
outline: none;
appearance: none;
-webkit-appearance: none;
border-radius: 50%;
cursor: pointer;
}

0 comments on commit a5ac893

Please sign in to comment.