频率到 RGB
Frequency to RGB
我正在寻找一种方法将 dynamic frequency
信号转换成相应的颜色。我找到了这个脚本 here:
<script type="text/javascript" src="data.js"></script>
<script type="text/javascript">
function trim1 (str) {
return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}
function decimalToHex(d) {
d = Math.round(d);
var hex = d.toString(16);
while (hex.length < 2) {
hex = "0" + hex;
}
return hex;
}
function freqToNM() {
freq = (document.getElementById("in").value);
Slight = 299792458;
convert();
}
function convert() {
var input = document.getElementById("in").value;
input = trim1(input);
var w = parseFloat(input);
if (w >= 380 && w < 440)
{
red = -(w - 440) / (440 - 380);
green = 0.0;
blue = 1.0;
}
else if (w >= 440 && w < 490)
{
red = 0.0;
green = (w - 440) / (490 - 440);
blue = 1.0;
}
else if (w >= 490 && w < 510)
{
red = 0.0;
green = 1.0;
blue = -(w - 510) / (510 - 490);
}
else if (w >= 510 && w < 580)
{
red = (w - 510) / (580 - 510);
green = 1.0;
blue = 0.0;
}
else if (w >= 580 && w < 645)
{
red = 1.0;
green = -(w - 645) / (645 - 580);
blue = 0.0;
}
else if (w >= 645 && w < 781)
{
red = 1.0;
green = 0.0;
blue = 0.0;
}
else
{
red = 0.0;
green = 0.0;
blue = 0.0;
}
// Let the intensity fall off near the vision limits
if (w >= 380 && w < 420)
factor = 0.3 + 0.7*(w - 380) / (420 - 380);
else if (w >= 420 && w < 701)
factor = 1.0;
else if (w >= 701 && w < 781)
factor = 0.3 + 0.7*(780 - w) / (780 - 700);
else
factor = 0.0;
var gamma = 0.80;
var R = (red > 0 ? 255*Math.pow(red * factor, gamma) : 0);
var G = (green > 0 ? 255*Math.pow(green * factor, gamma) : 0);
var B = (blue > 0 ? 255*Math.pow(blue * factor, gamma) : 0);
var hex = "#" + decimalToHex(R) + decimalToHex(G) + decimalToHex(B);
var output = hex;
document.getElementById("result").firstChild.nodeValue = output;
document.getElementById("result").style.color = hex;
}
</script>
我添加了 freqToNM 函数,试图在通过转换函数之前将以赫兹 (Hz) 为单位的频率转换为以纳米 (nm) 为单位的波长。有任何想法吗?提前致谢!
编辑:这是我如何应用它的html。
<!DOCTYPE html>
<head>
<title>Freqency to Color Converter</title>
</head>
<body>
<div id="main">
<p><input type="text" id = "in" name="in" size="20" tabindex="1" onKeyDown="if(event.keyCode==13) freqToNM();"/> <input type="button" value="Convert" name="B1" tabindex="2" onclick = "freqToNM()" /></p>
<p id="result"> </p>
<p> </p>
</div>
<script type="text/javascript" src="data.js"></script>
<script type="text/javascript">
function trim1 (str) {
return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}
function decimalToHex(d) {
d = Math.round(d);
var hex = d.toString(16);
while (hex.length < 2) {
hex = "0" + hex;
}
return hex;
}
function freqToNM() {
var freq = (document.getElementById("in").value);
var Slight = 299792458;
convert();
}
function convert() {
var input = Slight / freq;
input = trim1(input);
var w = parseFloat(input);
if (w >= 380 && w < 440)
{
red = -(w - 440) / (440 - 380);
green = 0.0;
blue = 1.0;
}
else if (w >= 440 && w < 490)
{
red = 0.0;
green = (w - 440) / (490 - 440);
blue = 1.0;
}
else if (w >= 490 && w < 510)
{
red = 0.0;
green = 1.0;
blue = -(w - 510) / (510 - 490);
}
else if (w >= 510 && w < 580)
{
red = (w - 510) / (580 - 510);
green = 1.0;
blue = 0.0;
}
else if (w >= 580 && w < 645)
{
red = 1.0;
green = -(w - 645) / (645 - 580);
blue = 0.0;
}
else if (w >= 645 && w < 781)
{
red = 1.0;
green = 0.0;
blue = 0.0;
}
else
{
red = 0.0;
green = 0.0;
blue = 0.0;
}
// Let the intensity fall off near the vision limits
if (w >= 380 && w < 420)
factor = 0.3 + 0.7*(w - 380) / (420 - 380);
else if (w >= 420 && w < 701)
factor = 1.0;
else if (w >= 701 && w < 781)
factor = 0.3 + 0.7*(780 - w) / (780 - 700);
else
factor = 0.0;
var gamma = 0.80;
var R = (red > 0 ? 255*Math.pow(red * factor, gamma) : 0);
var G = (green > 0 ? 255*Math.pow(green * factor, gamma) : 0);
var B = (blue > 0 ? 255*Math.pow(blue * factor, gamma) : 0);
var hex = "#" + decimalToHex(R) + decimalToHex(G) + decimalToHex(B);
var output = hex;
document.getElementById("result").firstChild.nodeValue = output;
document.getElementById("result").style.color = hex;
}
</script>
</body>
</html>
波长是速度/频率。当您将频率而不是波长传递给 convert
函数时,您必须将传递的频率更改为波长,如下所示:
function freqToNM(freq) {
// freq: The frequency as THz [float]
const Slight = 299792458;
return Slight / freq / 1000;
}
function convert() {
var input = document.getElementById("in").value;
input = trim1(input);
var f = parseFloat(input);
var w = freqToNM(f);
// The rest of the convert code
}
注意,freqToNM
returns 计算值,它不是调用 convert
函数。
我正在寻找一种方法将 dynamic frequency
信号转换成相应的颜色。我找到了这个脚本 here:
<script type="text/javascript" src="data.js"></script>
<script type="text/javascript">
function trim1 (str) {
return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}
function decimalToHex(d) {
d = Math.round(d);
var hex = d.toString(16);
while (hex.length < 2) {
hex = "0" + hex;
}
return hex;
}
function freqToNM() {
freq = (document.getElementById("in").value);
Slight = 299792458;
convert();
}
function convert() {
var input = document.getElementById("in").value;
input = trim1(input);
var w = parseFloat(input);
if (w >= 380 && w < 440)
{
red = -(w - 440) / (440 - 380);
green = 0.0;
blue = 1.0;
}
else if (w >= 440 && w < 490)
{
red = 0.0;
green = (w - 440) / (490 - 440);
blue = 1.0;
}
else if (w >= 490 && w < 510)
{
red = 0.0;
green = 1.0;
blue = -(w - 510) / (510 - 490);
}
else if (w >= 510 && w < 580)
{
red = (w - 510) / (580 - 510);
green = 1.0;
blue = 0.0;
}
else if (w >= 580 && w < 645)
{
red = 1.0;
green = -(w - 645) / (645 - 580);
blue = 0.0;
}
else if (w >= 645 && w < 781)
{
red = 1.0;
green = 0.0;
blue = 0.0;
}
else
{
red = 0.0;
green = 0.0;
blue = 0.0;
}
// Let the intensity fall off near the vision limits
if (w >= 380 && w < 420)
factor = 0.3 + 0.7*(w - 380) / (420 - 380);
else if (w >= 420 && w < 701)
factor = 1.0;
else if (w >= 701 && w < 781)
factor = 0.3 + 0.7*(780 - w) / (780 - 700);
else
factor = 0.0;
var gamma = 0.80;
var R = (red > 0 ? 255*Math.pow(red * factor, gamma) : 0);
var G = (green > 0 ? 255*Math.pow(green * factor, gamma) : 0);
var B = (blue > 0 ? 255*Math.pow(blue * factor, gamma) : 0);
var hex = "#" + decimalToHex(R) + decimalToHex(G) + decimalToHex(B);
var output = hex;
document.getElementById("result").firstChild.nodeValue = output;
document.getElementById("result").style.color = hex;
}
</script>
我添加了 freqToNM 函数,试图在通过转换函数之前将以赫兹 (Hz) 为单位的频率转换为以纳米 (nm) 为单位的波长。有任何想法吗?提前致谢!
编辑:这是我如何应用它的html。
<!DOCTYPE html>
<head>
<title>Freqency to Color Converter</title>
</head>
<body>
<div id="main">
<p><input type="text" id = "in" name="in" size="20" tabindex="1" onKeyDown="if(event.keyCode==13) freqToNM();"/> <input type="button" value="Convert" name="B1" tabindex="2" onclick = "freqToNM()" /></p>
<p id="result"> </p>
<p> </p>
</div>
<script type="text/javascript" src="data.js"></script>
<script type="text/javascript">
function trim1 (str) {
return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}
function decimalToHex(d) {
d = Math.round(d);
var hex = d.toString(16);
while (hex.length < 2) {
hex = "0" + hex;
}
return hex;
}
function freqToNM() {
var freq = (document.getElementById("in").value);
var Slight = 299792458;
convert();
}
function convert() {
var input = Slight / freq;
input = trim1(input);
var w = parseFloat(input);
if (w >= 380 && w < 440)
{
red = -(w - 440) / (440 - 380);
green = 0.0;
blue = 1.0;
}
else if (w >= 440 && w < 490)
{
red = 0.0;
green = (w - 440) / (490 - 440);
blue = 1.0;
}
else if (w >= 490 && w < 510)
{
red = 0.0;
green = 1.0;
blue = -(w - 510) / (510 - 490);
}
else if (w >= 510 && w < 580)
{
red = (w - 510) / (580 - 510);
green = 1.0;
blue = 0.0;
}
else if (w >= 580 && w < 645)
{
red = 1.0;
green = -(w - 645) / (645 - 580);
blue = 0.0;
}
else if (w >= 645 && w < 781)
{
red = 1.0;
green = 0.0;
blue = 0.0;
}
else
{
red = 0.0;
green = 0.0;
blue = 0.0;
}
// Let the intensity fall off near the vision limits
if (w >= 380 && w < 420)
factor = 0.3 + 0.7*(w - 380) / (420 - 380);
else if (w >= 420 && w < 701)
factor = 1.0;
else if (w >= 701 && w < 781)
factor = 0.3 + 0.7*(780 - w) / (780 - 700);
else
factor = 0.0;
var gamma = 0.80;
var R = (red > 0 ? 255*Math.pow(red * factor, gamma) : 0);
var G = (green > 0 ? 255*Math.pow(green * factor, gamma) : 0);
var B = (blue > 0 ? 255*Math.pow(blue * factor, gamma) : 0);
var hex = "#" + decimalToHex(R) + decimalToHex(G) + decimalToHex(B);
var output = hex;
document.getElementById("result").firstChild.nodeValue = output;
document.getElementById("result").style.color = hex;
}
</script>
</body>
</html>
波长是速度/频率。当您将频率而不是波长传递给 convert
函数时,您必须将传递的频率更改为波长,如下所示:
function freqToNM(freq) {
// freq: The frequency as THz [float]
const Slight = 299792458;
return Slight / freq / 1000;
}
function convert() {
var input = document.getElementById("in").value;
input = trim1(input);
var f = parseFloat(input);
var w = freqToNM(f);
// The rest of the convert code
}
注意,freqToNM
returns 计算值,它不是调用 convert
函数。