const err = Object.freeze(["Invalid param"]);
class CLR {
HSV_RGB(hue, sat, val){
//gaurd
if(!this.equal([typeof(hue), typeof(sat), typeof(val), "number"]) || !this.range(sat, 0, 1, true) || !this.range(val, 0, 1, true) || !this.range(hue, 0, 360, true))
throw err[0];
//imp vars
let chr = val * sat;
let hue_p = hue / 60;
let m = val - chr;
let x = chr * (1 - this.abs(hue_p % 2 - 1))
//return value
var temp;
//processing
switch(true) {
case this.range(hue_p, 0, 1, false) || (hue_p == 0):
temp = {red: chr, green: x, blue: 0};
break;
case this.range(hue_p, 1, 2, false) || (hue_p == 1):
temp = {red: x, green: chr, blue: 0};
break;
case this.range(hue_p, 2, 3, false) || (hue_p == 2):
temp = {red: 0, green: chr, blue: chr};
break;
case this.range(hue_p, 3, 4, false) || (hue_p == 3):
temp = {red: 0, green: x, blue: chr};
break;
case this.range(hue_p, 4, 5, false) || (hue_p == 4):
temp = {red: x, green: 0, blue: chr};
break;
case this.range(hue_p, 5, 6, false) || (hue_p == 5):
temp = {red: chr, green: 0, blue: x};
break;
}
return {red: (temp.red + m) * 255, green: (temp.green + m) * 255, blue: (temp.blue + m) * 255};
//parseInt(number, 10).toString(base) change base
}
//return absolute value
abs(val){
//gaurd
if(typeof(val) != "number")
throw err[0];
if(val < 0)
return -val;
else
return val;
}
//check whether all values are in range
range(val, l_bnd, u_bnd, inclusive){
if(inclusive) {
if(val <= u_bnd && val >= l_bnd)
return true;
}else {
if(val < u_bnd && val > l_bnd)
return true;
}
return false;
}
//check whether elements of array are all equal
equal(arr){
for(let i = 1; i < arr.length; i++)
if(arr[i] != arr[0])
return false;
return true;
}
}
let colour = new CLR();
//console.log(colour.equal([typeof(250), typeof(0.5), typeof(0.3), "number"]))
console.log(colour.HSV_RGB(250, 0.5, 0.3));