javascript - 我们如何使颜色比jquery中的给定颜色更暗

标签 javascript jquery css

<分区>

假设我的颜色是浅红色,我需要更多的红色形式,你能帮我看看这是怎么可能的吗?我有像 #00ff00 这样的十六进制颜色

谢谢

最佳答案

在这种情况下不需要 jQuery!

function ColorLuminance(hex, lum) {
    // validate hex string
    hex = String(hex).replace(/[^0-9a-f]/gi, '');
    if (hex.length < 6) {
        hex = hex[0]+hex[0]+hex[1]+hex[1]+hex[2]+hex[2];
    }
    lum = lum || 0;
    // convert to decimal and change luminosity
    var rgb = "#", c, i;
    for (i = 0; i < 3; i++) {
        c = parseInt(hex.substr(i*2,2), 16);
        c = Math.round(Math.min(Math.max(0, c + (c * lum)), 255)).toString(16);
        rgb += ("00"+c).substr(c.length);
    }
    return rgb;
}

hex为十六进制颜色值,lum为亮度因子,-1 <= lum <= 1,负数表示较暗,正数表示较亮

用法:

var newColor = ColorLuminance("#00ff00", -0.5); // "#334d66" - 50% darker

查看 working JSFiddle

来自 here 的代码片段

关于javascript - 我们如何使颜色比jquery中的给定颜色更暗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16787880/

相关文章:

javascript - 动态内容上的滑动切换

jquery - 如何在jquery中设置事件链接?

javascript - 使用按钮在可见和隐藏之间切换

javascript - 将今天的日期放在多个文本框中单击

javascript - 缺少文字游戏(填空)

javascript - 将异步/等待与 Mongoose 一起使用

javascript - 如何使用 JQuery 获取 DOM 元素的 id

javascript - 如何为轻量级图表设置自定义时区?

javascript - 我以为我定义了我的 jQuery 函数,但我不断收到 "is not defined"错误。为什么是这样?

css - DIV 布局不使用 Bootstrap 渲染