给定 RGB 值,如何创建色调(或阴影)?

发布于 2024-11-19 08:15:51 字数 71 浏览 4 评论 0原文

给定一个 RGB 值,例如 168, 0, 255,如何创建颜色的色调(使其更亮)和阴影(使其更暗)?

Given an RGB value, like 168, 0, 255, how do I create tints (make it lighter) and shades (make it darker) of the color?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无妨# 2024-11-26 08:15:51

几个用于着色和着色的选项

  • 对于阴影,将每个分量乘以其颜色的 1/4、1/2、3/4 等
    之前的值。系数越小,颜色越深。

  • 对于色调,计算(255 - 先前值),将其乘以 1/4,
    1/2、3/4 等(系数越大,色调越浅),并将其与之前的值相加(假设每个组件是 8 位整数)。

请注意,颜色操作(例如色调和其他阴影)应在线性 RGB 中完成。然而,文档中指定的或图像和视频中编码的 RGB 颜色不太可能是线性 RGB,在这种情况下,需要将所谓的逆传递函数应用于 RGB 颜色的每个分量。该函数随 RGB 色彩空间的不同而变化。例如,在 sRGB 颜色空间(如果 RGB 颜色空间未知,则可以假设),此函数大致相当于将每个 sRGB 颜色分量(范围从 0 到 1)的幂2.2. (请注意,“线性 RGB”不是 RGB 颜色空间。)

另请参阅 Violet Giraffe 关于“伽玛校正”的评论。

Among several options for shading and tinting:

  • For shades, multiply each component by 1/4, 1/2, 3/4, etc., of its
    previous value. The smaller the factor, the darker the shade.

  • For tints, calculate (255 - previous value), multiply that by 1/4,
    1/2, 3/4, etc. (the greater the factor, the lighter the tint), and add that to the previous value (assuming each.component is a 8-bit integer).

Note that color manipulations (such as tints and other shading) should be done in linear RGB. However, RGB colors specified in documents or encoded in images and video are not likely to be in linear RGB, in which case a so-called inverse transfer function needs to be applied to each of the RGB color's components. This function varies with the RGB color space. For example, in the sRGB color space (which can be assumed if the RGB color space is unknown), this function is roughly equivalent to raising each sRGB color component (ranging from 0 through 1) to a power of 2.2. (Note that "linear RGB" is not an RGB color space.)

See also Violet Giraffe's comment about "gamma correction".

旧夏天 2024-11-26 08:15:51

一些定义

  • 阴影是通过“加深”色调或“添加黑色”产生的
  • 色调是通过“淡化”色调或“添加白色”

产生阴影

根据您的颜色模型,可以使用不同的方法来创建较暗(阴影)或较亮(有色)颜色:

  • RGB:

    • 遮光:

      newR = currentR * (1 - 阴影系数)
      新G = 当前G * (1 - 阴影系数)
      新B = 当前B * (1 - 阴影系数)
      
    • 着色:

      newR = currentR + (255 - currentR) * 色调系数
      新G = 当前G + (255 - 当前G) * 色调系数
      新B = 当前B + (255 - 当前B) * 色调系数
      
    • 更一般地说,颜色导致颜色 RGB(currentR,currentG,currentB) 与颜色 RGBA(aR,aG,aB,alpha) 分层> 是:

      newR = 当前R + (aR - 当前R) * alpha
      新G = 当前G + (aG - 当前G) * alpha
      新B = 当前B + (aB - 当前B) * alpha
      

    其中 (aR,aG,aB) = black = (0,0,0) 用于阴影,(aR,aG,aB) = White = (255,255,255) code> 用于着色

  • HSVHSB

    • 要着色:降低 / 亮度或提高饱和度
    • 要着色:降低饱和度或增加 / 亮度
  • HSL
    • 遮光:降低亮度
    • 要着色:增加亮度

存在从一种颜色模型转换为另一种颜色模型的公式。根据您最初的问题,如果您处于 RGB 模式并希望使用 HSV 模型进行着色,则只需转换为 HSV 即可,进行着色并转换回 RGB。转换公式并不简单,但可以在互联网上找到。根据您的语言,它也可能作为核心功能提供:

比较模型

  • RGB 的优点是实现起来非常简单,但是:
    • 您只能相对地对颜色进行着色或着色
    • 您不知道您的颜色是否已经着色或着色
  • HSVHSB 有点复杂,因为您需要使用两个参数来获得你想要的(Saturation & Value / Brightness
  • HSL 是最好的我的观点:
    • 由 CSS3 支持(针对网络应用)
    • 简单而准确:
      • 50% 表示未改变的色调
      • >50% 表示色调较浅(色调)
      • <50% 表示色调较暗(阴影)
    • 给定一种颜色,您可以确定它是否已经着色或着色
    • 您可以相对或绝对地对颜色进行着色或着色(只需替换 Lightness 部分)

Some definitions

  • A shade is produced by "darkening" a hue or "adding black"
  • A tint is produced by "ligthening" a hue or "adding white"

Creating a tint or a shade

Depending on your Color Model, there are different methods to create a darker (shaded) or lighter (tinted) color:

  • RGB:

    • To shade:

      newR = currentR * (1 - shade_factor)
      newG = currentG * (1 - shade_factor)
      newB = currentB * (1 - shade_factor)
      
    • To tint:

      newR = currentR + (255 - currentR) * tint_factor
      newG = currentG + (255 - currentG) * tint_factor
      newB = currentB + (255 - currentB) * tint_factor
      
    • More generally, the color resulting in layering a color RGB(currentR,currentG,currentB) with a color RGBA(aR,aG,aB,alpha) is:

      newR = currentR + (aR - currentR) * alpha
      newG = currentG + (aG - currentG) * alpha
      newB = currentB + (aB - currentB) * alpha
      

    where (aR,aG,aB) = black = (0,0,0) for shading, and (aR,aG,aB) = white = (255,255,255) for tinting

  • HSV or HSB:

    • To shade: lower the Value / Brightness or increase the Saturation
    • To tint: lower the Saturation or increase the Value / Brightness
  • HSL:
    • To shade: lower the Lightness
    • To tint: increase the Lightness

There exists formulas to convert from one color model to another. As per your initial question, if you are in RGB and want to use the HSV model to shade for example, you can just convert to HSV, do the shading and convert back to RGB. Formula to convert are not trivial but can be found on the internet. Depending on your language, it might also be available as a core function :

Comparing the models

  • RGB has the advantage of being really simple to implement, but:
    • you can only shade or tint your color relatively
    • you have no idea if your color is already tinted or shaded
  • HSV or HSB is kind of complex because you need to play with two parameters to get what you want (Saturation & Value / Brightness)
  • HSL is the best from my point of view:
    • supported by CSS3 (for webapp)
    • simple and accurate:
      • 50% means an unaltered Hue
      • >50% means the Hue is lighter (tint)
      • <50% means the Hue is darker (shade)
    • given a color you can determine if it is already tinted or shaded
    • you can tint or shade a color relatively or absolutely (by just replacing the Lightness part)

℉絮湮 2024-11-26 08:15:51

我目前正在尝试画布和像素......我发现这种逻辑更适合我。

  1. 使用它来计算灰度(亮度?),
  2. 但使用现有值和新的“色调”值
  3. 计算差异(我发现我不需要相乘)
  4. 添加以抵消“色调”值< /p>

    var grey = (r + g + b) / 3;    
    var grey2 = (new_r + new_g + new_b) / 3;
    
    var dr = 灰色 - 灰色2 * 1;    
    var dg = 灰色 - 灰色2 * 1    
    var db = 灰色 - 灰色2 * 1;  
    
    色调_r = new_r + dr;    
    色调_g = new_g + dg;   
    色调_b = new_b _ db;
    

或类似的值...

I'm currently experimenting with canvas and pixels... I'm finding this logic works out for me better.

  1. Use this to calculate the grey-ness ( luma ? )
  2. but with both the existing value and the new 'tint' value
  3. calculate the difference ( I found I did not need to multiply )
  4. add to offset the 'tint' value

    var grey =  (r + g + b) / 3;    
    var grey2 = (new_r + new_g + new_b) / 3;
    
    var dr =  grey - grey2 * 1;    
    var dg =  grey - grey2 * 1    
    var db =  grey - grey2 * 1;  
    
    tint_r = new_r + dr;    
    tint_g = new_g + dg;   
    tint_b = new_b _ db;
    

or something like that...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文