数学转换sRGB和AdobeRGB [英] Mathematical conversion sRGB and AdobeRGB

查看:729
本文介绍了数学转换sRGB和AdobeRGB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个非常明确的问题,但是我做了很多研究,却没有找到答案。 this 与jpeg转换有关。 是关于python内置库的。 / p>

因此,如何将sRGB转换为AdobeRGB,反之亦然?我的意思是一个数学函数,它将3字节转换为3字节。没有jpges,依此类推。只是数学函数可以使用笔和纸来转换颜色。



是的,实际上Photoshop做到了,并且有一些奇怪的在线计算器可以显示另一个结果。



为什么我无法在Google中找到一个简单的公式?



我不得不思考,



如果有人能够描述,发生了什么或给出公式,我将非常感激。 / p>

更新



整数rgb的大型结果数组也是正确的答案。

解决方案

以下是实现公式的Python代码。如注释中所述,您从一种颜色空间转换为XYZ(规范化),然后从XYZ转换为新的颜色空间。我对这些功能的准确性不是100%满意,但是应该可以带您进入球场。当我提出优化时,我将它们编辑成答案。

  def linear_sRGB(c):
如果c< = 0.04045:
返回c / 12.92
其他:
return pow((c + 0.055)/ 1.055,2.4)

def sRGB_to_XYZn(r, g,b):
Rlin = linear_sRGB(r / 255.0)
Glin = linear_sRGB(g / 255.0)
Blin = linear_sRGB(b / 255.0)
Xn = Rlin * 0.4124 + Glin * 0.3576 + Blin * 0.1805
Yn = Rlin * 0.2126 + Glin * 0.7152 + Blin * 0.0722
Zn = Rlin * 0.0193 + Glin * 0.1192 + Blin * 0.9505
返回Xn,Yn ,Zn

def gamma_sRGB(c):
如果c< = 0.0031308:
返回12.92 * c
否则:
返回1.055 * pow( c,1 / 2.4)-0.055

def XYZn_to_sRGB(Xn,Yn,Zn):
Rlin = Xn * 3.2406255 + Yn * -1.53​​72080 + Zn * -0.4986286
Glin = Xn * -0.9689307 + Yn * 1.8757561 + Zn * 0.0415175
Blin = Xn * 0.0557101 + Yn * -0.2040211 + Zn * 1.0569959
R = round(255 * gamma_sRGB(Rlin))
G = round(255 * gamma_sRGB(Glin))
B = round(255 * gamma_sRGB(Blin))
return R,G,B

def linear_AdobeRGB(c):
如果c <= 0.0:
返回0.0
return pow(c,2.19921875)

def AdobeRGB_to_XYZn(R,G,B):
Rlin = linear_AdobeRGB(R / 255.0)
Glin = linear_AdobeRGB(G / 255.0)
Blin = linear_AdobeRGB(B / 255.0)
Xn = Rlin * 0.57667 + Glin * 0.18556 + Blin * 0.18823
Yn = Rlin * 0.29734 + Glin * 0.62736 + Blin * 0.07529
Zn = Rlin * 0.02703 + Glin * 0.07069 + Blin * 0.99134
返回Xn,Yn,Zn

def gamma_AdobeRGB(c):
如果c <= 0.0:
返回0.0
return pow(c,1 / 2.19921875)

def XYZn_to_AdobeRGB(Xn,Yn,Zn):
Rlin = Xn * 2.04159 + Yn * -0.56501 + Zn * -0.34473
Glin = Xn * -0.96924 + Yn * 1.87597 + Zn * 0.04156
Blin = Xn * 0.01344 + Yn * -0.11836 + Zn * 1.01517
R = round(255 * gamma_AdobeRGB(Rlin))
G = round(255 * gamma_AdobeRGB(Glin))
B = round(255 * gamma_AdobeRGB(Blin))
return R,G,B


It is very clear question, but I've done a lot of research and didn't find answer. StackOverflow question as this or this are about jpeg converting. This is about python build-in library.

So, how to convert sRGB to AdobeRGB and vice versa??? I mean a mathematical function, that convert 3 bytes to 3 bytes. No jpges, and so on. Just mathematical function to convert colors using pen and paper.

Yes, photoshop does it in fact and there are some strange online calculators, that show another result.

Why can't I find a simple formula in google?

I got to thinking, that I don't know something and there is no straight answer to my question.

I will be very grateful if someone can describe, what's going on or give formula.

UPDATE

Large array of results for integer rgbs will be also correct answer.

解决方案

Here is Python code to implement the formulas. As noted in the comments, you convert from one color space to XYZ (normalized) then from XYZ to the new color space. I'm not 100% happy with the accuracy of these functions, but it should get you in the ballpark. As I come up with refinements I'll edit them into the answer.

def linear_sRGB(c):
    if c <= 0.04045:
        return c / 12.92
    else:
        return pow((c + 0.055) / 1.055, 2.4)

def sRGB_to_XYZn(r, g, b):
    Rlin = linear_sRGB(r / 255.0)
    Glin = linear_sRGB(g / 255.0)
    Blin = linear_sRGB(b / 255.0)
    Xn = Rlin * 0.4124 + Glin * 0.3576 + Blin * 0.1805
    Yn = Rlin * 0.2126 + Glin * 0.7152 + Blin * 0.0722
    Zn = Rlin * 0.0193 + Glin * 0.1192 + Blin * 0.9505
    return Xn, Yn, Zn

def gamma_sRGB(c):
    if c <= 0.0031308:
        return 12.92 * c
    else:
        return 1.055 * pow(c, 1/2.4) - 0.055

def XYZn_to_sRGB(Xn, Yn, Zn):
    Rlin = Xn * 3.2406255 + Yn *-1.5372080 + Zn *-0.4986286
    Glin = Xn *-0.9689307 + Yn * 1.8757561 + Zn * 0.0415175
    Blin = Xn * 0.0557101 + Yn *-0.2040211 + Zn * 1.0569959
    R = round(255 * gamma_sRGB(Rlin))
    G = round(255 * gamma_sRGB(Glin))
    B = round(255 * gamma_sRGB(Blin))
    return R, G, B

def linear_AdobeRGB(c):
    if c <= 0.0:
        return 0.0
    return pow(c, 2.19921875)

def AdobeRGB_to_XYZn(R, G, B):
    Rlin = linear_AdobeRGB(R / 255.0)
    Glin = linear_AdobeRGB(G / 255.0)
    Blin = linear_AdobeRGB(B / 255.0)
    Xn = Rlin * 0.57667 + Glin * 0.18556 + Blin * 0.18823
    Yn = Rlin * 0.29734 + Glin * 0.62736 + Blin * 0.07529
    Zn = Rlin * 0.02703 + Glin * 0.07069 + Blin * 0.99134
    return Xn, Yn, Zn

def gamma_AdobeRGB(c):
    if c <= 0.0:
        return 0.0
    return pow(c, 1/2.19921875)

def XYZn_to_AdobeRGB(Xn, Yn, Zn):
    Rlin = Xn * 2.04159 + Yn *-0.56501 + Zn *-0.34473
    Glin = Xn *-0.96924 + Yn * 1.87597 + Zn * 0.04156
    Blin = Xn * 0.01344 + Yn *-0.11836 + Zn * 1.01517
    R = round(255 * gamma_AdobeRGB(Rlin))
    G = round(255 * gamma_AdobeRGB(Glin))
    B = round(255 * gamma_AdobeRGB(Blin))
    return R, G, B

这篇关于数学转换sRGB和AdobeRGB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆