实现目标
本文使用RGB(Red,Green,Blue)颜色空间与HSV(Hue,Saturation,Value)颜色空间的相互转换。输入、输出数据皆为RGB格式,所以需要先将RGB转换成HSV,调整S值后再将HSV转换回RGB。
功能分为三个模块:
RGB2HSV
本文使用12bitRGB,RGB转HSV的公式如下:
设定
C
l
i
p
N
u
m
=
2
12
−
1
=
4095
ClipNum = 2^{12}-1=4095
ClipNum=212−1=4095
R
e
d
,
∈
[
0
−
4095
]
G
r
e
e
n
,
∈
[
0
−
4095
]
B
l
u
e
,
∈
[
0
−
4095
]
C
m
a
x
=
m
a
x
(
R
e
d
,
G
r
e
e
n
,
B
l
u
e
)
,
∈
[
0
−
4095
]
C
m
i
n
=
m
i
n
(
R
e
d
,
G
r
e
e
n
,
B
l
u
e
)
,
∈
[
0
−
4095
]
Δ
=
C
m
a
x
−
C
m
i
n
,
∈
[
0
−
4095
]
V
a
l
u
e
=
C
m
a
x
,
∈
[
0
−
4095
]
\begin{aligned}&Red&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&,\in[0-4095] \cr &Green&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&,\in[0-4095] \cr &Blue&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&,\in[0-4095] \cr &C_{max}=max(Red,Green,Blue)&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&,\in[0-4095] \cr &C_{min}=min(Red,Green,Blue)&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&,\in[0-4095] \cr &\Delta=C_{max}-C_{min}&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&,\in[0-4095] \cr &Value=C_{max}&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&,\in[0-4095] \end{aligned}
RedGreenBlueCmax=max(Red,Green,Blue)Cmin=min(Red,Green,Blue)Δ=Cmax−CminValue=Cmax,∈[0−4095],∈[0−4095],∈[0−4095],∈[0−4095],∈[0−4095],∈[0−4095],∈[0−4095]
S a t u r a t i o n = { 0 C m a x = 0 Δ ∗ 4096 C m a x C m a x ≠ 0 , ∈ [ 0 − 4096 ] ( 4096 会强制拉成 4095 以保证 12 b i t 不溢出 ) Saturation =\begin{aligned}&\left\{ \begin{aligned} &0 & & C_{max}=0 \cr &\frac{\Delta*4096}{C_{max}} & &C_{max}\ne 0\end{aligned} \right.&&&,\in[0-4096]\end{aligned}(4096会强制拉成4095以保证12bit不溢出) Saturation=⎩ ⎨ ⎧0CmaxΔ∗4096Cmax=0Cmax=0,∈[0−4096](4096会强制拉成4095以保证12bit不溢出)
H u e = { 0 Δ = 0 ( ( G r e e n − B l u e ) ∗ 4096 Δ ) × 682 4096 C m a x = R e d , G r e e n ≥ B l u e , ∈ [ 0 − 4096 6 ] ( ( G r e e n − B l u e ) ∗ 4096 Δ + 6 × 4096 ) × 682 4096 C m a x = R e d , G r e e n < B l u e , ∈ [ 5 6 × 4096 − 4095 ] ( ( B l u e − R e d ) ∗ 4096 Δ + 2 × 4096 ) × 682 4096 C m a x = G r e e n , ∈ [ 4096 6 − 3 6 × 4096 ] ( ( R e d − G r e e n ) ∗ 4096 Δ + 4 × 4096 ) × 682 4096 C m a x = B l u e , ∈ [ 3 6 × 4096 − 5 6 × 4096 ] Hue =\left\{ \begin{aligned} &0 & & \Delta=0 & \cr &(\frac{(Green-Blue)*4096}{\Delta})\times\frac{682}{4096}& &C_{max}=Red,Green≥Blue &&,\in[0-\frac{4096}{6}] \cr &(\frac{(Green-Blue)*4096}{\Delta}+6\times4096)\times\frac{682}{4096}& &C_{max}=Red,Green<Blue &&,\in[\frac{5}{6}\times4096-4095] \cr &(\frac{(Blue-Red)*4096}{\Delta}+2\times4096)\times\frac{682}{4096}& &C_{max}=Green&&,\in[\frac{4096}{6}-\frac{3}{6}\times4096] \cr &(\frac{(Red-Green)*4096}{\Delta}+4\times4096)\times\frac{682}{4096}& &C_{max}=Blue&&,\in[\frac{3}{6}\times4096-\frac{5}{6}\times4096]\end{aligned} \right. Hue=⎩ ⎨ ⎧0(Δ(Green−Blue)∗4096)×4096682(Δ(Green−Blue)∗4096+6×4096)×4096682(Δ(Blue−Red)∗4096+2×4096)×4096682(Δ(Red−Green)∗4096+4×4096)×4096682Δ=0Cmax=Red,Green≥BlueCmax=Red,Green<BlueCmax=GreenCmax=Blue,∈[0−64096],∈[65×4096−4095],∈[64096−63×4096],∈[63×4096−65×4096]
WEIGHT
Saturation取值范围为 [ 0 − 4095 ] [0-4095] [0−4095],使用一个4096深度的RAM做查找表(LUT, Look Up Table),预先存储需要调整后的Saturation值,用原Saturation做读地址,读出的数据作为新的Saturation
HSV2RGB
本文使用12bitHSV,HSV转RGB的公式如下:
H
u
e
,
∈
[
0
−
4095
]
H
′
=
H
u
e
4096
×
360
,
∈
[
0
−
360
)
S
a
t
u
r
a
t
i
o
n
,
∈
[
0
−
4095
]
S
′
=
S
a
t
u
r
a
t
i
o
n
4096
,
∈
[
0
−
1
)
V
a
l
u
e
,
∈
[
0
−
4095
]
h
i
=
H
′
60
=
H
u
e
4096
×
360
60
=
H
u
e
×
6
4096
,
∈
[
0
−
5
]
f
=
H
u
e
×
6
−
h
i
×
4096
,
∈
[
0
−
4095
]
f
′
=
f
4096
,
∈
[
0
−
1
)
T
e
m
p
=
4095
−
S
a
t
u
r
a
t
i
o
n
,
,
∈
[
0
−
4095
]
T
e
m
p
′
=
T
e
m
p
4096
,
∈
[
0
−
1
)
\begin{aligned}&Hue&,\in[0-4095]&&&H'=\frac{Hue}{4096}\times360&&,\in[0-360) \cr &Saturation&,\in[0-4095]&&&S'=\frac{Saturation}{4096}&&,\in[0-1) \cr &Value&,\in[0-4095] \cr&h_{i}=\frac{H'}{60}=\frac{Hue}{4096}\times\frac{360}{60}=\frac{Hue\times6}{4096}&,\in[0-5] \cr&f=Hue\times6-h_{i}\times4096&,\in[0-4095]&&&f'=\frac{f}{4096}&&,\in[0-1) \cr&Temp=4095-Saturation,&,\in[0-4095]&&&Temp'=\frac{Temp}{4096}&&,\in[0-1) \end{aligned}
HueSaturationValuehi=60H′=4096Hue×60360=4096Hue×6f=Hue×6−hi×4096Temp=4095−Saturation,,∈[0−4095],∈[0−4095],∈[0−4095],∈[0−5],∈[0−4095],∈[0−4095]H′=4096Hue×360S′=4096Saturationf′=4096fTemp′=4096Temp,∈[0−360),∈[0−1),∈[0−1),∈[0−1)
p
=
V
a
l
u
e
×
(
1
−
S
′
)
=
V
a
l
u
e
×
T
e
m
p
′
=
V
a
l
u
e
×
T
e
m
p
4096
,
∈
[
0
−
4095
]
A
d
j
u
s
t
=
f
′
×
(
V
a
l
u
e
−
V
a
l
u
e
×
(
1
−
S
′
)
)
=
f
4096
×
(
V
a
l
u
e
−
p
)
,
∈
[
0
−
4095
]
q
=
V
a
l
u
e
×
(
1
−
f
′
×
S
′
)
=
V
a
l
u
e
−
f
′
×
(
V
a
l
u
e
−
V
a
l
u
e
×
(
1
−
S
′
)
)
=
V
a
l
u
e
−
A
d
j
u
s
t
,
,
∈
[
0
−
4095
]
t
=
V
a
l
u
e
×
(
1
−
(
1
−
f
′
)
×
S
′
)
=
V
a
l
u
e
−
V
a
l
u
e
×
S
′
+
V
a
l
u
e
×
S
′
×
f
′
=
V
a
l
u
e
×
(
1
−
S
′
)
+
f
′
×
(
V
a
l
u
e
−
V
a
l
u
e
×
(
1
−
S
′
)
)
=
p
+
A
d
j
u
s
t
,
,
∈
[
0
−
4095
]
\begin{aligned}\cr&p=Value\times(1-S')=Value\times Temp'=Value\times\frac{Temp}{4096}&,\in[0-4095] \cr&Adjust= f' \times(Value-Value\times(1-S'))=\frac{f}{4096}\times(Value-p)&,\in[0-4095] \cr&q=Value\times(1-f'\times S')=Value-f'\times(Value-Value\times(1-S'))=Value-Adjust,&,\in[0-4095] \cr&t=Value\times(1-(1-f')\times S')=Value-Value\times S'+Value\times S'\times f' \cr&=Value\times(1-S')+ f' \times(Value-Value\times(1-S'))=p+Adjust,&,\in[0-4095] \end{aligned}
p=Value×(1−S′)=Value×Temp′=Value×4096TempAdjust=f′×(Value−Value×(1−S′))=4096f×(Value−p)q=Value×(1−f′×S′)=Value−f′×(Value−Value×(1−S′))=Value−Adjust,t=Value×(1−(1−f′)×S′)=Value−Value×S′+Value×S′×f′=Value×(1−S′)+f′×(Value−Value×(1−S′))=p+Adjust,,∈[0−4095],∈[0−4095],∈[0−4095],∈[0−4095]
( R e d , G r e e n , B l u e ) = { ( V a l u e , t , p ) , h i = 0 ( q , V a l u e , p ) , h i = 1 ( p , V a l u e , t ) , h i = 2 ( p , q , V a l u e ) , h i = 3 ( t , p , V a l u e ) , h i = 4 ( V a l u e , p , q ) , h i = 5 , ∈ [ 0 − 4095 ] (Red,Green,Blue)=\begin{aligned}&\left\{ \begin{aligned} &(Value,t,p) && ,h_{i}=0 \cr &(q,Value,p)&& ,h_{i}=1 \cr &(p,Value,t)&& ,h_{i}=2 \cr &(p,q,Value)&& ,h_{i}=3 \cr &(t,p,Value)&& ,h_{i}=4 \cr &(Value,p,q)&& ,h_{i}=5 \end{aligned} \right.&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&,\in[0-4095]\end{aligned} (Red,Green,Blue)=⎩ ⎨ ⎧(Value,t,p)(q,Value,p)(p,Value,t)(p,q,Value)(t,p,Value)(Value,p,q),hi=0,hi=1,hi=2,hi=3,hi=4,hi=5,∈[0−4095]
参考文献
[1]:FPGA实现RGB与HSV的转换
[2]:三分钟带你快速学习RGB、HSV和HSL颜色空间