Module:YCbCr to RGB
local p = {}
-- Conversion matrices (YCbCr to RGB)
local matrices = {
["BT.601"] = {
kr = 0.299,
kb = 0.114
},
["BT.709"] = {
kr = 0.2126,
kb = 0.0722
},
["BT.2020"] = {
kr = 0.2627,
kb = 0.0593
}
}
local function clamp(x, min, max)
if x < min then return min end
if x > max then return max end
return x
end
-- sRGB gamma encoding
local function linearToSRGB(c)
if c <= 0 then return 0 end
if c <= 0.0031308 then
return 12.92 * c
else
return 1.055 * (c ^ (1/2.4)) - 0.055
end
end
function p.convert(frame)
local args = frame:getParent().args
local Y = tonumber(args.Y) or 0
local Cb = tonumber(args.Cb) or 0
local Cr = tonumber(args.Cr) or 0
local standard = args.standard or "BT.601"
local bitdepth = tonumber(args.bitdepth) or 8
local range = args.range or "limited"
local format = args.format or "hex"
local m = matrices[standard] or matrices["BT.601"]
local maxValue = (2 ^ bitdepth) - 1
local Yf, Cbf, Crf
if range == "full" then
Yf = Y / maxValue
Cbf = (Cb / maxValue) - 0.5
Crf = (Cr / maxValue) - 0.5
else
-- Limited (TV range)
local offsetY = 16 * (2 ^ (bitdepth - 8))
local offsetC = 128 * (2 ^ (bitdepth - 8))
local scaleY = 219 * (2 ^ (bitdepth - 8))
local scaleC = 224 * (2 ^ (bitdepth - 8))
Yf = (Y - offsetY) / scaleY
Cbf = (Cb - offsetC) / scaleC
Crf = (Cr - offsetC) / scaleC
end
local kr = m.kr
local kb = m.kb
local kg = 1 - kr - kb
-- Linear RGB
local R_lin = Yf + (2 - 2 * kr) * Crf
local B_lin = Yf + (2 - 2 * kb) * Cbf
local G_lin = Yf - ( (2 * kb * (1 - kb) / kg) * Cbf )
- ( (2 * kr * (1 - kr) / kg) * Crf )
-- Gamma encode
local R = clamp(linearToSRGB(R_lin), 0, 1)
local G = clamp(linearToSRGB(G_lin), 0, 1)
local B = clamp(linearToSRGB(B_lin), 0, 1)
local r8 = math.floor(R * 255 + 0.5)
local g8 = math.floor(G * 255 + 0.5)
local b8 = math.floor(B * 255 + 0.5)
local hex = string.format("#%02X%02X%02X", r8, g8, b8)
if format == "swatch" then
return string.format(
'<span style="display:inline-block;width:1.5em;height:1.5em;border:1px solid #000;background-color:%s;"></span> %s',
hex, hex
)
else
return "#" .. string.sub(hex:gsub("\n",""):gsub("%s+",""), 2)
end
end
return p
Content Disclaimer
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.
- The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
- There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
- It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
- Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
- Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.