从任何RGB组合中获取颜色名称(包括脚本) [英] Get a color name from any RGB combination (script included)

查看:78
本文介绍了从任何RGB组合中获取颜色名称(包括脚本)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从我的应用程序中返回十六进制和RGB颜色(来自camara),我想使用脚本来识别该颜色并显示名称...我发现了 http://gauth.fr/2011/09/从任何rgb组合中获取颜色名称/脚本,但我无法使其正常工作...

i am returning a hex and an RGB color in my app (from camara) and i want to use a script to identify that color and display a name... ive found http://gauth.fr/2011/09/get-a-color-name-from-any-rgb-combination/ script but i cant make it to work ...

我的代码具有此脚本来获取rgb和hex值:

my code has this script to get the rgb and hex values :

function color(){
 var rgb = getAverageRGB(document.getElementById('smallImage'));
    document.getElementById("r").style.background = 'rgb('+rgb.r+','+rgb.g+','+rgb.b+')';

var x='rgb('+rgb.r+','+rgb.g+','+rgb.b+')';
document.querySelector('.results').innerHTML = 'rgb('+rgb.r+','+rgb.g+','+rgb.b+')' + " hex:"+ hexafy(x); showDiv1()  ;

然后我尝试使用链接页面上脚本中的代码...但是没有成功...:

then i tryed to use the code from the script on the linked page...but with no sucess... :

<script>
window.classifier = new ColorClassifier();
get_dataset('js/dataset.js', function (data){
    window.classifier.learn(data);
});
var result_name = window.classifier.classify("#aaf000");
alert (result_name);
</script>

我还包括:

<script type="text/javascript" src="js/color_classifier.js"></script>
        <script type="text/javascript" src="js/dataset.js"></script>

但要注意发生了...如何使用此提示?

but noting happens... any clue how to use this ?

推荐答案

由于没有足够的细节,很难给您一个明确的答案-但是,如果您正确地调用了两个外部资源(如果需要,您还需要jQuery'正在使用此方法),那么您应该收到所请求的警报.当您将资源内联(不包括jQuery)时,它的一个小提琴可以正常工作. https://jsfiddle.net/j3gbwvvg/.您不需要jQuery的原因是因为我们将数据直接放入变量中并调用了它,而不是使用利用了.getJSON()jQuery函数的库函数.

Hard to give you a definitive answer as there's not enough detail - but if you're calling the two external resources correctly (you'll also need jQuery if you're using this method), then you should get an alert as requested. Here's a fiddle of it working correctly when you bring the resources inline (which excludes the need for jQuery) https://jsfiddle.net/j3gbwvvg/. The reason you don't need jQuery is because we're putting the data directly into a variable and calling that, not using the library's function which utilises the .getJSON() jQuery function.

window.classifier = new ColorClassifier();
window.classifier.learn(data);
var result_name = window.classifier.classify("#aaf000");
alert(result_name);

这篇关于从任何RGB组合中获取颜色名称(包括脚本)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆