有没有一种方法可以使用函数或api来获取颜色的类型



我正在尝试创建自己的网站,我需要一些东西来告诉我输入是rgb值还是十六进制值,或者任何其他值,如cmyk或hsl。我已经被困了一天了。是否有API或函数可以帮助查找所使用的颜色类型?

功能理念:

function determineType(string) {
//code
}
const rgbValue = '187, 72, 198'
const hexValue = 'ff0000'
const cmykValue = "73%, 45%, 0%, 4%"
determineType(rgbValue) //rgb
determineType(hexValue) //hex
determineType(cmykValue) //cmyk

当然,若有一个API可以帮助,那个将是非常好的知道。

这可能适用于

function determineType(s){
let split=s.split(',');
if(split=='')return;
let len=split.length;
return len==3?'RGB':
len==1?'HEX':
len==4?'CYMK': null
}
determineType('187, 72, 198'); // "RGB"
determineType('ff0000'); // "HEX"
determineType('73%, 45%, 0%, 4%'); // "CYMK"
determineType(''); // undefined

最新更新