HexMaster Code

Last week I released my first project, a very simple color quiz game.
In this post I want to show you how I made it.

What I used

Initially I created this project from scratch on a blank webpage, but then I decided to integrate it inside my site, so I convert some of the css and html code to work with Bootstrap(I use it on this site).
I made this chage as I think it looks better.
Responsive image

HTML

The first thing I did was loading the style:

1<link rel="stylesheet" href="/css/HexMasterStyle.css">

Then I created the big banner:

 1<section id="header" class="jumbotron jumbotron-fluid text-center">
 2    <div class="container d-flex justify-content-center">
 3        <div class="row">
 4            <div class="col">
 5                <h1 class="display-4 mt-5">Can you recreate this color?
 6                    <br>
 7                    HEX:
 8                    <span id="rndColor">#FFAA66</span>
 9                </h1>
10            </div>
11        </div>
12      </div>
13</section>

The id of the span will let me to edit its content via javascript.

After that I create 3 Bootstrap row.
Responsive image
The first row is pretty simple, just some text and a colorBox to show the correct color.

1<div class="row justify-content-end">
2        <div class="col-sm-3">
3            <p>the color should be like this:</p>
4        </div>
5        <div class="col-sm-2">
6            <div class="colorBox"></div>
7        </div>
8    </div>

The second row is even simpler as it only show the accuracy of your’s selected color:

1<div class="row justify-content-end">
2        <div class="col-sm-2">
3            <p style="margin: auto 7% auto auto;" id="accText"><p>
4        </div>
5    </div>

The third row is a little more complex, as it store the 3 bars, the button and the selected color:

 1<div class="row justify-content-md-end">
 2        <div class="col-sm-6 mt-5">
 3            <span class="bar">
 4                <input type="range" id="redSlider" value="25">
 5            </span>
 6            <span class="bar">
 7                <input type="range" id="greenSlider" value="25">
 8            </span>
 9            <span class="bar">
10                <input type="range" id="blueSlider" value="25">
11            </span>
12        </div>
13        <div class="col-sm-2">
14            <input type="button" id="doneBtn" value="Done!" style="display: block;">
15            <input type="button" id="resetBtn" value="New color!" style="display: none;">
16        </div>
17        <div class="col-sm-2 ml-5">
18            <p>Your color is: </p>
19            <div class="colorBox"></div>
20        </div>
21    </div>

CSS

The CSS is a little tricky as I want to have the range input to be vertically(if the screen width is more than 1000px), then I customized them to be the red, green and blue bars.
Searching on internet i found this very userful for this project.

The base style of the range inputs is:

 1input[type="range"] { 
 2    -webkit-appearance: none;
 3    overflow: hidden;
 4    cursor: pointer;
 5    border-radius: 0; /* iOS */
 6    border: 2px solid dodgerblue;
 7    transform: rotate(-90deg);
 8}
 9
10@media only screen and (max-width: 1000px) {
11    input[type="range"] { 
12        transform: rotate(0deg);
13        width: 200px;
14    }
15}
16
17::-webkit-slider-runnable-track {
18    background: #ffffff;
19}
20
21::-webkit-slider-thumb {
22    -webkit-appearance: none;
23    width: 0px;
24    height: 40px;
25    background: dodgerblue;
26}
27
28::-moz-range-track {
29    height: 40px;
30    background: #ffffff;
31}
32
33::-moz-range-thumb {
34    background: #fff;
35    height: 40px;
36    width: 0px;
37}

To create the bars I styled the range input like this:

 1/* red slider */
 2#redSlider {
 3    border: 2px solid #ca4141;
 4}
 5
 6#redSlider::-webkit-slider-thumb {
 7    box-shadow: -100vw 0 0 100vw #ca4141;
 8}
 9#redSlider::-moz-range-thumb {
10    box-shadow: -100vw 0 0 100vw #ca4141;
11}
12/* green slider */
13#greenSlider {
14    border: 2px solid #4CAF50;
15}
16
17#greenSlider::-webkit-slider-thumb {
18    box-shadow: -100vw 0 0 100vw #4CAF50;
19}
20#greenSlider::-moz-range-thumb {
21    box-shadow: -100vw 0 0 100vw #4CAF50;
22}
23/* blue slider */
24#blueSlider {
25    border: 2px solid #1c66bb;
26}
27
28#blueSlider::-webkit-slider-thumb {
29    box-shadow: -100vw 0 0 100vw #1c66bb;
30}
31#blueSlider::-moz-range-thumb {
32    box-shadow: -100vw 0 0 100vw #1c66bb;
33}
34
35.bar {
36    width:15%;
37}

Then I create the style for the colorBox and the button(s):

 1.colorBox {
 2    height: 100px;
 3    margin: 20px;
 4    -webkit-transition: all 0.3s;
 5    -moz-transition: all 0.3s;
 6    transition: all 0.3s;
 7}
 8
 9p {
10    line-height: 1.1;
11    font-weight: 600;
12}
13
14#doneBtn {
15    background-color: #4CAF50;
16    border: none;
17    color: white;
18    padding: 15px 32px;
19    text-align: center;
20    text-decoration: none;
21    display: inline-block;
22    font-size: 16px;
23    margin: 4px 2px;
24    cursor: pointer;
25}
26
27#resetBtn {
28    background-color: rgb(29, 159, 192);
29    border: none;
30    color: white;
31    padding: 15px 32px;
32    text-align: center;
33    text-decoration: none;
34    display: inline-block;
35    font-size: 16px;
36    margin: 4px 2px;
37    cursor: pointer;
38}

JavaScript

This is the most interesting part of the project, and I had a lot of fun making it.
In the first part I just set all the variable, and I called 2 function, genColor and updateText:

1var quiz_color;
2var user_color;
3var accuracy;
4var done_btn = document.querySelector("#doneBtn");
5var reset_btn = document.querySelector("#resetBtn");
6
7genColor();
8updateText(false);

With the function genColor I can create a random color, I first create a random number for each channel(r,g,b) then I assign these 3 value to the quiz_color array.

1function genColor() {
2    var r = Math.floor(Math.random() * 256);
3    var g = Math.floor(Math.random() * 256);
4    var b = Math.floor(Math.random() * 256);
5    quiz_color = [r, g, b];
6}

With the updateText function I take each channel of the “quiz_color” array and, as it is written in decimal base, I convert it to hexadecimal base. Then I update the shown text of the HTML element that have the “rndColor” id.
I used a bool to know if it must show or not the correct color, this only happen when the player click the Done button.

1function updateText(show){
2    new_text = "#" + quiz_color[0].toString(16) + " " + quiz_color[1].toString(16) + " " + quiz_color[2].toString(16);
3    document.querySelector("#rndColor").textContent = new_text;
4    if(show){
5        colorBox(0,quiz_color[0],quiz_color[1],quiz_color[2]);
6    }
7}

The “colorBox” function simply set the color of the boxes:

1function colorBox(n,r,g,b){
2    document.querySelectorAll(".colorBox")[n].style.backgroundColor = "rgb(" + r + ", " + g + ", " + b + ")";
3}

Then I added the event listener to the 2 button:

 1done_btn.addEventListener("click", function(){
 2    var value_r = document.querySelectorAll(".bar")[0].querySelector("input").value / 100 *255;
 3    var value_g = document.querySelectorAll(".bar")[1].querySelector("input").value / 100 *255;
 4    var value_b = document.querySelectorAll(".bar")[2].querySelector("input").value / 100 *255;
 5    user_color = [value_r,value_g,value_b];
 6    colorBox(1,value_r,value_g,value_b);
 7    colorDifference();
 8    disableButton(false);
 9});
10
11reset_btn.addEventListener("click", function(){
12    colorBox(0,255,255,255);
13    colorBox(1,255,255,255);
14    genColor();
15    updateText(false);
16    document.querySelector("#accText").textContent = "";
17    disableButton(true);
18});

The first one is the Done button, it display the result of the combination of the 3 bars, then it call the colorDifference function and disable itself as it will be replaced by the new color! button, this one simply make the game restart.

The “colorDifference” function is where I calculated the average differences( in %) between the player’s color and the correct color:

1function colorDifference(){
2
3    var result = Math.abs((user_color[0]/255)-(quiz_color[0]/255)) + Math.abs((user_color[1]/255)-(quiz_color[1]/255)) + Math.abs((user_color[2]/255)-(quiz_color[2]/255));
4    result = 100-(result/3*100);
5    document.querySelector("#accText").textContent = "Accuracy: " + result.toFixed(2) + " %";
6    updateText(true);
7}

What it does is pretty simple, it take the user color and the correct color, then instead of having a value that can be from “0” to “255” it make these values(for every channel) go between “0” and “1”, then I subtract to get the difference.
After that it sum the difference of each channel, and finally calculate the average, then calculate the % of the value.

The last part is only used to avoid to have both buttons enabled at the same time:

 1function disableButton(inverse){
 2    if(!inverse){
 3        done_btn.style.display = "none";
 4        reset_btn.style.display = "block";
 5    }
 6    else{
 7        done_btn.style.display = "block";
 8        reset_btn.style.display = "none";
 9    }
10}

Conclusion

Even if it’s just a simple game I’m very satisfied of what I achieved.
What do you think about it?
Did you find this post interesting?
Comment on Twitter! @CodeSailer