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

Photoshop API颜色转Hex码不符:是归一化错误还是API数据问题?

问题分析与解决

核心原因:RGB数据位深误解

Photoshop API返回的RGB值是15位范围(0-32767),而非你代码中假设的16位范围(0-65535)。对比两组数值就能发现:

  • Photoshop显示的#0A86B6对应16位RGB:
    • Red: 10 × (65535/255) ≈ 2570
    • Green: 134 × (65535/255) ≈ 34502
    • Blue: 182 × (65535/255) ≈ 46766
  • API返回的数值恰好是上述结果的一半:1306≈2570/2,17251≈34502/2,23383≈46766/2,正好对应15位的最大值32767(65535的一半)。

修正后的代码

将归一化时的分母从65536改为32767,就能得到和Photoshop一致的结果:

rgb_values = {
  "blue": 23383,
  "green": 17251,
  "red": 1306
}

def normalize_and_convert_to_hex(rgb_values):
    red = round((rgb_values["red"] / 32767) * 255)
    green = round((rgb_values["green"] / 32767) * 255)
    blue = round((rgb_values["blue"] / 32767) * 255)
    return f"#{red:02x}{green:02x}{blue:02x}"

x = normalize_and_convert_to_hex(rgb_values)
print(x)  # 输出 #0a86b6,与Photoshop一致

结论

既不是归一化逻辑错误(逻辑本身没问题,但适配的位深不对),也不是API返回数据有误——只是你误将15位RGB数据当成了16位处理,调整分母后即可匹配Photoshop显示的Hex码。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 23:48:17