【发布时间】:2019-10-18 08:00:20
【问题描述】:
我找到了this 代码,该代码从 波长变为 rgb。
function wavelengthToColor(wavelength) {
var r,
g,
b,
alpha,
colorSpace,
wl = wavelength,
gamma = 1;
if (wl >= 380 && wl < 440) {
R = -1 * (wl - 440) / (440 - 380);
G = 0;
B = 1;
} else if (wl >= 440 && wl < 490) {
R = 0;
G = (wl - 440) / (490 - 440);
B = 1;
} else if (wl >= 490 && wl < 510) {
R = 0;
G = 1;
B = -1 * (wl - 510) / (510 - 490);
} else if (wl >= 510 && wl < 580) {
R = (wl - 510) / (580 - 510);
G = 1;
B = 0;
} else if (wl >= 580 && wl < 645) {
R = 1;
G = -1 * (wl - 645) / (645 - 580);
B = 0.0;
} else if (wl >= 645 && wl <= 780) {
R = 1;
G = 0;
B = 0;
} else {
R = 0;
G = 0;
B = 0;
}
// intensty is lower at the edges of the visible spectrum.
if (wl > 780 || wl < 380) {
alpha = 0;
} else if (wl > 700) {
alpha = (780 - wl) / (780 - 700);
} else if (wl < 420) {
alpha = (wl - 380) / (420 - 380);
} else {
alpha = 1;
}
colorSpace = ["rgba(" + (R * 100) + "%," + (G * 100) + "%," + (B * 100) + "%, " + alpha + ")", R, G, B, alpha]
// colorSpace is an array with 5 elements.
// The first element is the complete code as a string.
// Use colorSpace[0] as is to display the desired color.
// use the last four elements alone or together to access each of the individual r, g, b and a channels.
return colorSpace;
}
然后this 似乎 说可以将 RGB(或就此而言,十六进制或任何其他计算机颜色格式)转换为波长,但我无法理解/关注该帖子。想知道是否可以在 JavaScript 中解释/展示如何将 hex/rgb/hsl/etc 至少转换为近似值。 (计算机颜色)值以纳米为单位的波长。
我想取一个十六进制颜色并最终找到它的波长,同样地取一个波长并找到它的十六进制。
如果不可能做到精确,那么任何粗略的近似都可以。我只是试图获取科学波长值并将它们转换为十六进制,并同样为十六进制值找到一个粗略的波长范围,以显示(大致)该十六进制颜色的科学对象。
【问题讨论】:
-
并非 RGB 色域中的所有颜色都对应于特定频率的辐射能量。
-
无论哪种方式,在这种情况下,即使是 一些 的转换也会起作用。
-
@LancePollard 看看电磁频谱。例如,没有紫色。黑色的波长是多少?我们看到的大多数颜色是多个波长以及不同强度的组合,无法在单个波长中捕捉到。
-
例如我们称之为“紫色”的颜色。陆生动物的颜色敏感细胞对不同的频带做出反应,进一步的“处理”将频率组合表现为特定体验(当然取决于所涉及的生物体)
-
基本上 color 非常复杂。强烈推荐 Arthur Zajonc 的书Catching The Light。
标签: javascript colors