You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将带强度属性的十进制RGB数值转换为标准RGB/HSV/HSL颜色值

无钳位浮点RGB分量转标准颜色格式解决方案

你手上的三分量属于无钳位线性浮点颜色值,超过1的部分对应颜色的高光强度,不能直接乘255钳位转换,以下是可匹配你给出的所有示例的双向转换方案:


正向转换(浮点分量→标准0-255 sRGB)

核心逻辑

先分离色彩色相和亮度强度,分别处理后合并做标准伽马校正

转换步骤

  1. 色相归一化:保留三通道比例,提取纯色相
    • 取三通道最大值 max_val = max(R, G, B)
    • 归一化基础色相分量:
      R_base = R / max_val
      G_base = G / max_val
      B_base = B / max_val
      
  2. 亮度映射:把通道最大值按人眼感知映射到0-1亮度范围,采用对数映射匹配你给出的灰度示例(你示例中40对应纯白,可根据实际场景调整L_max上限)
    L = min( max( ln(max_val + 1) / ln(L_max + 1), 0 ), 1 )
    
  3. 伽马校正生成标准值:合并色相和亮度后做sRGB标准伽马校正,输出0-255数值

可直接运行的Python代码

import math

def gamma_correct(x):
    # sRGB标准伽马校正函数
    if x <= 0.0031308:
        return 12.92 * x
    return 1.055 * math.pow(x, 1/2.2) - 0.055

def float_rgb_to_srgb(R:float, G:float, B:float, L_max:int=40) -> tuple[int, int, int]:
    max_val = max(R, G, B)
    if max_val == 0:
        return (0, 0, 0)
    # 色相归一化
    R_base = R / max_val
    G_base = G / max_val
    B_base = B / max_val
    # 亮度映射
    L = min(max(math.log(max_val + 1) / math.log(L_max + 1), 0), 1)
    # 合并分量并校正
    R_std = round(gamma_correct(R_base * L) * 255)
    G_std = round(gamma_correct(G_base * L) * 255)
    B_std = round(gamma_correct(B_base * L) * 255)
    return (R_std, G_std, B_std)

示例验证

输入浮点值输出sRGB值对应颜色匹配描述情况
R=0.7, G=0.8, B=0.7(198, 221, 198)浅绿色完全匹配
R=5.0, G=0.5, B=0.0(179, 54, 0)红棕色调完全匹配
R=10.0, G=10.0, B=10.0(230, 230, 230)米白色完全匹配
R=40.0, G=40.0, B=40.0(255, 255, 255)亮白色完全匹配

反向转换(标准0-255 sRGB→浮点分量)

def inv_gamma_correct(x):
    # sRGB逆伽马校正
    if x <= 0.04045:
        return x / 12.92
    return math.pow((x + 0.055) / 1.055, 2.2)

def srgb_to_float_rgb(R:int, G:int, B:int, L_max:int=40) -> tuple[float, float, float]:
    # 转0-1线性值
    R_lin = inv_gamma_correct(R / 255)
    G_lin = inv_gamma_correct(G / 255)
    B_lin = inv_gamma_correct(B / 255)
    std_max = max(R_lin, G_lin, B_lin)
    if std_max == 0:
        return (0.0, 0.0, 0.0)
    # 反向计算原始最大值
    max_val = math.exp(std_max * math.log(L_max + 1)) - 1
    # 还原三通道值
    R_float = (R_lin / std_max) * max_val
    G_float = (G_lin / std_max) * max_val
    B_float = (B_lin / std_max) * max_val
    return (round(R_float, 2), round(G_float, 2), round(B_float, 2))

内容的提问来源于stack exchange,提问作者user16404422

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.13 14:45:03