- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
给定 HSL 中的某种颜色(假设为 hsl(74,64%,59%)),我想计算哪种较深的阴影(具有相同的 h 和 s 值)给我足够的对比度来满足 W3C 颜色对比度要求。
有一些公式可以将 HSL 转换为 RGB(例如 https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_RGB )并根据该 RGB 计算相对亮度(例如 https://www.w3.org/TR/WCAG20/#relativeluminancedef )。根据颜色对比度公式 ( https://www.w3.org/TR/WCAG20/#contrast-ratiodef ),我可以计算出其他颜色的相对亮度应该是多少。
但是,然后我被卡住了。我发现无法从给定的相对亮度计算回给定 h 和 s 的 HSL 颜色。
使用类似 https://contrast-ratio.com/ 的工具我可以降低亮度直到满足要求,但我想要一个公式(最好在 JavaScript 中)来对大量颜色进行计算。
(我目前正在使用二进制搜索方法来找到最接近的值,通过测试从 HSL 到 RGB 到相对亮度的许多转换,但这是非常密集的,而且我想知道在两者之间转换为 RGB 是否会引入不准确。)
最佳答案
希望这是你需要的
使用此 SO answer 中的公式,及以下:
// Relative luminance calculations
function adjustGamma(p) {
if (p <= 0.03928) {
return p / 12.92;
} else {
return Math.pow( ( p + 0.055 ) / 1.055, 2.4 );
}
}
function relativeLuminance(rgb) {
const r = adjustGamma( rgb[0] / 255 );
const g = adjustGamma( rgb[1] / 255 );
const b = adjustGamma( rgb[2] / 255 );
return 0.2126 * r + 0.7152 * g + 0.0722 * b;
}
// Contrast calculations
function contrastRatio(a,b) {
const ratio = (a + 0.05) / (b + 0.05);
return ratio >= 1 ? ratio : 1 / ratio;
}
// Loop for correct lightness
function rgbFromHslContrast(h, s, l1, ratio) {
var inc = -0.01;
var l2 = ( ( l1 + 0.05 ) / ratio - 0.05 );
if (l2 < 0) {
l2 = ( ratio * ( l1 + 0.05 ) - 0.05 );
inc = -inc;
}
while (contrastRatio(l1, relativeLuminance(hslToRgb(h, s, l2))) < ratio) {
l2 += inc;
}
return hslToRgb(h, s, l2);
}
const originalHslAsRgb = hslToRgb(0.2, 0.2, 0.2);
const l1 = relativeLuminance(originalHslAsRgb);
const contrastRgb = rgbFromHslContrast(0.2, 0.2, l1, 3.5) // 3.5 is minimum contrast factor we target for..
// [139, 149, 100]
// equivalent to hsl(72, 20%, 53%)
关于hsl - 从相对亮度转换为 HSL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61525100/
给定 HSL 中的某种颜色(假设为 hsl(74,64%,59%)),我想计算哪种较深的阴影(具有相同的 h 和 s 值)给我足够的对比度来满足 W3C 颜色对比度要求。 有一些公式可以将 HSL 转
我正在编写一个程序,允许用户在 RGB 中选择各种前景色和背景色。我不想让他们选择太相似的前景和背景,并决定转换为 HSL 并使用 HSL 欧几里德距离作为检查相似性的一种方式。 HSL 空间是否有很
这是我当前用来更改图像的色相、饱和度和亮度的代码。我如何只改变饱和度或亮度而不添加色调?或者如何找到像素的当前色调值以便我可以使用它? public static final int MAX_
我最近一直在开发一个颜色选择器应用程序,它允许用户选择颜色的色调、饱和度和亮度。在用户决定颜色后,我会为用户提供与所选颜色相匹配的不同配色方案。给出的一些配色方案是免费的、拆分免费的、三合会的、类似的
我真的是 jquery 的新手,我正在尝试使用它,以便每次为单词闪烁生成的颜色总是不同的颜色,但无法弄清楚如何这样做。请查看下面我的代码或查看 jsfiddle。 http://jsfiddle.ne
我正在尝试为图像编写色相/饱和度/亮度滤镜(使用 C++)。 RGB->HSL 转换工作正常,但当考虑到每个像素具有不同的初始饱和度和亮度这一事实时,我的问题就来了。 对于每个像素,我计算了源 HSL
我不清楚 HSL 颜色的书写方式。 W3 有这样的: color: hsl(120, 100%, 50%); 而 ThreeJs 是这样要求它们的: h — hue value between 0.0
我还没有找到一种算法/实现可以将 rgb(0, 96, 255) 转换为 HSL 并返回,而不会以稍微不同的值结束 (rgb(0 , 98, 255))。这意味着每个人的算法都被破坏了(包括 Wolf
我有一个复杂的问题,但可以通过这个小问题解决。我想制作一个按像素工作的着色器。 我为图片定义了一个基色(图片中有一些像素具有这种颜色,还有很多其他像素接近这种颜色): Hex: #188DD9 HSL
我使用 LESS ,我想利用各种集成的 color functions 只允许设置几个基本颜色,然后派生其他改变色相,饱和度,亮度,旋转,ecc。 假设我们的着色器中有以下 2 种颜色(在本例中为浅绿
当我尝试使用 jQuery Color 进行简单的 HSL 到 RGB 转换时, $.colors('hsl(100,100%,50%)').toString('rgb'); 我注意到HSL结构中有一
我在使用 Javascript/jQuery 时遇到了一个奇怪的问题。我需要使用下面编写的函数使用 HSL 颜色模型更新背景颜色: function updateColorPreviewHSV(hsv
我正在构建一个星型可视化引擎,需要对从 API 接收到的值进行插值。 HSL 色标是: -.63, hsl: 228° 100% 80%, .165, hsl: 224° 100% 90%, .33,
我正在尝试解析 hsl 颜色字符串并从中获取十六进制颜色字符串。我尝试使用正则表达式但无法弄清楚。我的正则表达式应该如何匹配和解析 hsl 颜色字符串到色调、饱和度和值字段。输入将是以下之一; -
我发现的奇怪的事情。将 #579f2f 转换为 hsl 时,我得到了 hsl(99, 54%, 40%);。将其转换回十六进制时,我得到 #559d2f。我使用各种在线工具对此进行了测试。 我想原因是
有什么方法可以使用 hsl 只调整特定的值吗?例如,如果我只想改变饱和度或亮度并保持色调不变? .red { background-color: #ff0000; /* or, hsl(0,1
在下面显示的代码中,颜色的 hsl 色调分量具有与预期不同的值。 对于:r:255 g:168 b:177 它给出了 353 的 hsl 色调,但在其他网络工具上它产生了 354 QColor rgb
由于 HSL vals 可能会在彩虹中循环,并且在那个众所周知的色调数组 (ROYGBIV) 中有 7 种基本颜色,每个颜色之间的距离,从 0 到 360,应该是 51 或 52。那么为什么这里的颜色
我的问题与Jon P提到的问题不同在这个问题中question pointed to by Jon P并在上面标记为已经提出的问题。这个问题是要显示整个温度梯度。这道题是将单一的色调附加到单一的温度上
如果我有一个 OpenGL 纹理,并且我需要在渲染纹理之前对其执行 HSL 修改,据我所知,我需要一个着色器。问题是,我对着色器一无所知。有谁知道我需要看哪里? 我想编写一个函数,我可以在其中传递一个
我是一名优秀的程序员,十分优秀!