Skip to content

Commit be00569

Browse files
authored
Merge pull request #23 from chingu-voyages/maya
Maya
2 parents aa68c8f + 630e4d7 commit be00569

File tree

4 files changed

+246
-0
lines changed

4 files changed

+246
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,4 @@ coding!
4242
- Teammate name #5: Yusuke: [GitHub](https://github.com/yusuken1121) / [LinkedIn](https://www.linkedin.com/in/yusuke-nagaoka-a602242a8/)
4343
- Teammate name #6 Maya Power: [GitHub](https://github.com/maya17power) / [LinkedIn](https://www.linkedin.com/in/#)
4444
- Teammate name #7 Ronaldo Shrestha: [GitHub](https://github.com/Ron1373) / [LinkedIn](https://www.linkedin.com/in/ronaldo-shrestha-170255128/)
45+

index.html

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1" />
7+
<title>Color Palette</title>
8+
9+
<!--JQUERY LIBRARY -->
10+
<script defer src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
11+
12+
<!-- FONTAWSOME -->
13+
<script defer src="https://kit.fontawesome.com/df21ef76d6.js" crossorigin="anonymous"></script>
14+
15+
<!-- BOOTSTRAP CSS -->
16+
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet"
17+
integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous" />
18+
<script defer src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"
19+
integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL"
20+
crossorigin="anonymous"></script>
21+
22+
<!-- LOCAL SOURCE -->
23+
<link rel="stylesheet" type="text/css" href="style.css" />
24+
<script defer type="module" src="main.js"></script>
25+
</head>
26+
27+
<body>
28+
<div class="container-fluid text-center" id="colorWheel">
29+
Color Wheel
30+
<div id="colorWheel-container" class="container-fluid hsl-container"></div>
31+
<div id="sample-container" class="sample-container">
32+
<p class="text">
33+
<span id="hsl-values" style="display: none">Hue: <b>0</b>, Saturation: <b>100</b>%, Lightness:
34+
<b>50</b>%</span>
35+
</p>
36+
<div id="sample" class="sample"></div>
37+
</div>
38+
</div>
39+
<div class="container-fluid text-center" id="colorArray">
40+
Color Picked Array
41+
<div class="d-flex align-items-start justify-content-center" id="color-array">
42+
</div>
43+
</div>
44+
</body>
45+
46+
</html>

main.js

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
//Output of colors to page element
2+
const colorArray = document.querySelector("#color-array");
3+
//console.log(colorArray);
4+
5+
//array of colors example:testing
6+
const colors = [];
7+
8+
//length of array
9+
const arrayLength = colors.length;
10+
console.log(arrayLength);
11+
12+
//iterate of color array
13+
//create new div element with card and colorCard class
14+
// const iterate = colors.map((el) => {
15+
// const newCard = document.createElement("div");
16+
// newCard.className = "card colorCard btn-close";
17+
// newCard.id = `${colors.indexOf(el)}`;
18+
// newCard.style = `background-color: ${el}`;
19+
// colorArray.appendChild(newCard);
20+
// });
21+
22+
///////////////CRUD
23+
24+
//delete box
25+
colorArray.addEventListener('click', (el) => {
26+
console.log(el.target);
27+
colorArray.removeChild(el.target);
28+
});
29+
30+
//colorWheel Container
31+
let parent = document.getElementById("colorWheel-container");
32+
33+
createHslPicker(
34+
parent,
35+
(h, s, l) => {
36+
let sample = document.getElementById("sample"),
37+
text = document.getElementById("hsl-values");
38+
39+
sample.style.background = `hsl(${h}, ${s}%, ${l}%)`;
40+
//text.innerHTML = `Hue: <b>${h}</b>, Saturation: <b>${s}</b>%, Lightness: <b>${l}</b>%`;
41+
},
42+
50
43+
);
44+
45+
/*
46+
Main function, creates the HSL picker inside a parent that you provide (such as a div).
47+
As the user picks different HSL values, you are notified via the callback.
48+
The HSL values are provided as arguments, and you can use them to update other parts of your UI.
49+
You can also pass an initial hue, via the thrid argument.
50+
*/
51+
function createHslPicker(parent, callback,initialHue = 50) {
52+
parent.innerHTML = getHtml();
53+
54+
let canvas = document.getElementById("canvas-hue"),
55+
rgSat = document.getElementById("rg-saturation"),
56+
rgLight = document.getElementById("rg-lightness"),
57+
hsl = [initialHue, 100, 50];
58+
59+
drawColorWheel();
60+
onHslChanged();
61+
62+
let xCircle = canvas.width / 2,
63+
yCircle = canvas.height / 2,
64+
radius = canvas.width / 2;
65+
66+
canvas.addEventListener("mousemove", (ev) => {
67+
let dist = Math.sqrt(
68+
Math.pow(ev.offsetX - xCircle, 2) + Math.pow(ev.offsetY - yCircle, 2)
69+
);
70+
canvas.style.cursor = dist <= radius ? "cell" : "default";
71+
});
72+
73+
canvas.addEventListener("mousedown", (ev) => {
74+
if (ev.button != 0) {
75+
return;
76+
}
77+
78+
let dist = Math.sqrt(
79+
Math.pow(ev.offsetX - xCircle, 2) + Math.pow(ev.offsetY - yCircle, 2)
80+
);
81+
82+
if (radius < dist) {
83+
return;
84+
}
85+
86+
let sine = (yCircle - ev.offsetY) / dist,
87+
radians = Math.atan2(yCircle - ev.offsetY, ev.offsetX - xCircle);
88+
89+
if (radians < 0) {
90+
radians = 2 * Math.PI - Math.abs(radians);
91+
}
92+
93+
let degrees = (radians * 180) / Math.PI,
94+
hue = Math.round(degrees);
95+
onHuePicked(hue);
96+
});
97+
98+
function onHuePicked(h) {
99+
hsl[0] = h;
100+
//convert to hex
101+
let hex = hslToHex(hsl[0], 100, 50);
102+
console.log(hex);
103+
//add hex to array
104+
colors.push(hex);
105+
//init change on new hex
106+
onHslChanged(hex);
107+
}
108+
109+
function hslToHex(h, s, l) {
110+
l /= 100;
111+
const a = (s * Math.min(l, 1 - l)) / 100;
112+
const f = (n) => {
113+
const k = (n + h / 30) % 12;
114+
const color = l - a * Math.max(Math.min(k - 3, 9 - k, 1), -1);
115+
return Math.round(255 * color)
116+
.toString(16)
117+
.padStart(2, "0"); // convert to Hex and prefix "0" if needed
118+
};
119+
return `#${f(0)}${f(8)}${f(4)}`;
120+
}
121+
122+
function onHslChanged() {
123+
if (callback) {
124+
let hsl_to_hex = hslToHex(hsl[0], 100, 50);
125+
callback(...hsl_to_hex);
126+
genColorCube(hsl_to_hex);
127+
}
128+
}
129+
130+
function genColorCube(hex){
131+
const newCard = document.createElement("div");
132+
newCard.className = "card colorCard btn-close";
133+
newCard.id = `${colors.length}`;
134+
newCard.style = `background-color: ${hex}`;
135+
colorArray.appendChild(newCard);
136+
};
137+
138+
function drawColorWheel() {
139+
let ctx = canvas.getContext("2d"),
140+
radius = canvas.width / 2,
141+
x = canvas.width / 2,
142+
y = canvas.height / 2,
143+
[h, s, l] = hsl;
144+
145+
for (let i = 0; i < 360; i++) {
146+
let color = `hsl(${i}, ${s}%, ${l}%)`;
147+
148+
ctx.beginPath();
149+
150+
ctx.moveTo(x, y);
151+
ctx.arc(x, y, radius, (-(i + 1) * Math.PI) / 180, (-i * Math.PI) / 180);
152+
ctx.lineTo(x, y);
153+
ctx.closePath();
154+
155+
ctx.fillStyle = color;
156+
ctx.strokeStyle = color;
157+
158+
ctx.fill();
159+
ctx.stroke();
160+
}
161+
}
162+
163+
function getHtml() {
164+
return `<div>
165+
<div>
166+
<canvas id="canvas-hue" width="300" height="300"></canvas>
167+
</div>
168+
169+
<div style="grid-area: saturation; padding-top: 30px; padding-left: 20px;">
170+
<input id="rg-saturation" style="display:none;">
171+
</div>
172+
173+
<div style="grid-area: lightness; padding-top: 30px; padding-left: 20px;">
174+
<input id="rg-lightness" style="display: none;">
175+
</div>
176+
</div>`;
177+
}
178+
}
179+

style.css

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
.container{
2+
background-color: bisque;
3+
height: 100px;
4+
width: auto auto;
5+
}
6+
.colorCard{
7+
height: 50px;
8+
width: 50px;
9+
margin: 0;
10+
}
11+
#colorWheel{
12+
height: 200px;
13+
width: auto auto;
14+
margin-top: 20px;
15+
margin-bottom: 20px;
16+
}
17+
#colorArray{
18+
margin-top: 20px;
19+
margin-bottom: 20px;
20+
}

0 commit comments

Comments
 (0)