For each checkbox that is checked, I am trying to run the function below for changing the color to white / black depending on the darkness of the background-color. However the third checkbox is supposed to be dark enough to change the text color to white, but it isn't executing.
function isDark(color) {
var match = /rgb\((\d+).*?(\d+).*?(\d+)\)/.exec(color);
return (
(match[1] & 255) + (match[2] & 255) + (match[3] & 255) < 3 * 256 / 2
);
}
Trying to call using:
label.css(
"color",
isDark(label.css("background-color")) ? "white" : "black"
);
Here is a reference codepen with the function working on its own:
https://codepen.io/moofawsaw/pen/RmZWaL
$(document).ready(function() {
function isDark(color) {
var match = /rgb\((\d+).*?(\d+).*?(\d+)\)/.exec(color);
return (match[1] & 255) + (match[2] & 255) + (match[3] & 255) < 3 * 256 / 2;
}
$(".item").each(function() {
var item = $(this).find(".input");
var color = $(this)
.find("label")
.attr("data-color");
item.change(function() {
var label = $(this).closest(".item");
var dot = $(this)
.closest(".item")
.find(".dot");
var cancel = $(this)
.closest(".item")
.find(".cancel");
if ($(this).prop("checked")) {
var val = 0;
dot.css({
"-webkit-transform": "scale(" + val + ")",
"-moz-transform": "scale(" + val + ")",
"-ms-transform": "scale(" + val + ")",
"-o-transform": "scale(" + val + ")",
transform: "scale(" + val + ")",
width: "0px"
});
label.css("background-color", color);
cancel.css("display", "flex");
label.css(
"color",
isDark(label.css("background-color")) ? "white" : "black"
);
} else {
var val = 1;
label.css("background-color", "");
dot.css({
"-webkit-transform": "scale(" + val + ")",
"-moz-transform": "scale(" + val + ")",
"-ms-transform": "scale(" + val + ")",
"-o-transform": "scale(" + val + ")",
transform: "scale(" + val + ")",
width: "12px"
});
cancel.css("display", "none");
label.css("color", "");
}
});
});
});
label {
display: flex;
align-items: center;
margin-left: 6px;
font-size: 12px;
line-height: 16px;
font-weight: 400;
padding: 4px 0;
font-family: Roboto Mono, arial, sans-serif;
letter-spacing: -0.2px;
user-select: none;
cursor: pointer;
transition: all 0.25s ease;
}
input {
display: none;
visibility: hidden;
}
.boxes {
display: flex;
padding: 3rem
}
.item {
background-color: white;
padding-left: 8px !important;
padding-right: 8px !important;
border: 1px solid #dadce0;
border-radius: 6px;
display: flex;
align-items: center;
margin: .3rem;
transition-property: all;
transition-duration: 0.1s;
transition-timing-function: ease-in-out;
}
.cancel {
padding: 0px 6px;
display: none;
transition: all 0.25s ease;
}
.dot {
border-radius: 50%;
width: 12px;
height: 12px;
transition: all 0.25s ease;
}
.item:nth-child(1) .dot {
background: rgb(255, 64, 129);
}
.item:nth-child(2) .dot {
background: rgb(49, 231, 182);
}
.item:nth-child(3) .dot {
background: blue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="boxes">
<div class="item">
<span class="dot"></span>
<label data-color="rgb(255, 64, 129)" class="checkbox" for="one">
<input class="input" id="one" type="checkbox">Lobster
<i class="far fa-times cancel"></i>
</label>
</div>
<div class="item">
<span class="dot"></span>
<label data-color="rgb(49, 231, 182)" class="checkbox" for="two">
<input class="input" id="two" type="checkbox">Tuna
<div class="cancel">x</div>
</label>
</div>
<div class="item">
<span class="dot"></span>
<label data-color="blue" class="checkbox" for="three">
<input class="input" id="three" type="checkbox">Pine
<div class="cancel">x</div>
</label>
</div>
</div>
return (match[0] & 255) + (match[1] & 255) + (match[2] & 255) < 3 * 256 / 2;