// Your code:
/*function FillColor(r,g,b,a){
if (r > 255){r = 255;}
if (g > 255){g = 255;}
if (b > 255){b = 255;}
if (a > 1){a = 1;}
if (r < 0){r = 0;}
if (g < 0){g = 0;}
if (b < 0){b = 0;}
if (a < 0){a = 0;}
ctx.fillStyle = "rgba(" + r + "," + g + "," + b + "," + a + ")";
}*/
// Updated function (assuming you can use ES6/ES2015):
function FillColor(r, g, b, a) {
const rgb = (v) => v < 0 ? 0 : v > 255 ? 255 : v;
const alpha = a < 0 ? 0 : a > 1 ? 1 : a;
// You can replace this "return" with your "ctx.fillStyle", but it doesn't work for this example to log the values out.
return `rgba(${rgb(r)}, ${rgb(g)}, ${rgb(b)}, ${a})`;
}
// Updated function (assuming you are bound to ES5):
function FillColorEs5(r, g, b, a) {
var rgb = function(v) {
return v < 0 ? 0 : v > 255 ? 255 : v;
}
var alpha = a < 0 ? 0 : a > 1 ? 1 : a;
// Same thing here...you can replace the "return" with your "ctx.fillStyle" instead.
return "rgba(" + rgb(r) + ", " + rgb(g) + ", " + rgb(b) + ", " + alpha + ")";
}
// Sample running function:
console.log(FillColor(1, 2, 354, 0.1));
console.log(FillColorEs5(1, 2, 354, 0.1));