V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
KimigaiiWuyi
V2EX  ›  Python

请教一下各位,如何通过纯色背景的 RGB 值,确定文字颜色的 RGB 值呢?

  •  
  •   KimigaiiWuyi ·
    KimigaiiWuyi · 2021-12-13 14:52:56 +08:00 · 2002 次点击
    这是一个创建于 864 天前的主题,其中的信息可能已经有所发展或是发生改变。

    显示效果清晰,好看,至少不会觉得配色恶心 单纯取互补色感觉是不行了,有没有什么已经实现的算法之类的可以用用

    9 条回复    2021-12-13 16:46:10 +08:00
    KimigaiiWuyi
        1
    KimigaiiWuyi  
    OP
       2021-12-13 14:54:21 +08:00
    https://www.toptal.com/designers/colourcode/
    这个网站似乎实现的不错
    vanton
        2
    vanton  
       2021-12-13 14:58:09 +08:00
    ```javascript
    /**
    * 高对比度颜色
    * @param {Color} [color]
    * @returns {string} "#000000"|"#ffffff"
    */
    function contrastingColor(color = { r: 0, g: 0, b: 0 }) {
    let luma = 0.2126 * color.r + 0.7152 * color.g + 0.0722 * color.b;
    return luma > 128 ? "#000000" : "#ffffff";
    }
    ```
    KimigaiiWuyi
        3
    KimigaiiWuyi  
    OP
       2021-12-13 15:06:18 +08:00
    @vanton 0.2126 * color.r + 0.7152 * color.g + 0.0722 * color.b;这个好像是取灰度值,该怎么通过灰度值判定文字颜色的 RGB 呢
    makelove
        4
    makelove  
       2021-12-13 15:22:00 +08:00
    直接 rgba(0,0,0, ?)不好吗?
    Material Design 也是这么搞的
    KimigaiiWuyi
        5
    KimigaiiWuyi  
    OP
       2021-12-13 15:26:51 +08:00
    @makelove 这样暗色背景不是完全不能看了,而且主要就是想 colorful 一点
    zhyl
        6
    zhyl  
       2021-12-13 15:44:48 +08:00
    @KimigaiiWuyi
    这个网站文字颜色的计算方法

    "rgb-to-fgc": function(n) {
    var r, o;
    return o = converter.bounds.validate("rgb", n),
    r = 96 / 255,
    Math.max(o.r, o.g, o.b) > 1 - r && (r *= -1),
    converter.bounds.validate("rgb", {
    r: o.r + r,
    g: o.g + r,
    b: o.b + r
    })
    },
    KimigaiiWuyi
        7
    KimigaiiWuyi  
    OP
       2021-12-13 16:03:57 +08:00
    @zhyl 感谢,我尝试一下
    makelove
        8
    makelove  
       2021-12-13 16:05:56 +08:00
    @KimigaiiWuyi 暗色不就是把 0 换成 255? 想不同点就把 0 稍小改一下
    zhyl
        9
    zhyl  
       2021-12-13 16:46:10 +08:00   ❤️ 3
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4498 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 04:06 · PVG 12:06 · LAX 21:06 · JFK 00:06
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.