Skip to content

Commit

Permalink
Implement various minor corrections from "Sarifuddin (2021)" to "HCL"…
Browse files Browse the repository at this point in the history
… colourspace.
  • Loading branch information
KelSolaar committed Jan 27, 2023
1 parent 082bb06 commit 1a580ef
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 20 deletions.
9 changes: 8 additions & 1 deletion BIBLIOGRAPHY.bib
Expand Up @@ -2388,7 +2388,14 @@ @misc{Sarifuddin2005
@misc{Sarifuddin2005a,
title = {{{HCL}}: A New {{Color Space}} for a More {{Effective Content-based Image Retrieval}}},
author = {Sarifuddin, Madenda and Missaoui, Rokia},
year = {2005}
year = {2005},
file = {/Users/kelsolaar/Zotero/storage/E38KGE54/Sarifuddin and Missaoui - 2005 - HCL a new Color Space for a more Effective Conten.pdf}
}

@misc{Sarifuddin2021,
title = {{{RGB}} to {{HCL}} and {{HCL}} to {{RGB}} Color Conversion},
author = {Sarifuddin, Madenda},
year = {2021}
}

@misc{Sastanina,
Expand Down
43 changes: 24 additions & 19 deletions colour/models/rgb/cylindrical.py
Expand Up @@ -38,6 +38,9 @@
- :cite:`Sarifuddin2005a` : Sarifuddin, M., & Missaoui, R. (2005). HCL: a new
Color Space for a more Effective Content-based Image Retrieval.
http://w3.uqo.ca/missaoui/Publications/TRColorSpace.zip
- :cite:`Sarifuddin2021` : Sarifuddin, M. (2021). RGB to HCL and HCL to RGB
color conversion (1.0.0). https://www.mathworks.com/matlabcentral/\
fileexchange/100878-rgb-to-hcl-and-hcl-to-rgb-color-conversion
- :cite:`Wikipedia2015` : Wikipedia. (2015). HCL color space. Retrieved
April 4, 2021, from https://en.wikipedia.org/wiki/HCL_color_space
"""
Expand Down Expand Up @@ -396,8 +399,9 @@ def RGB_to_HCL(
| ``HCL`` | [0, 1] | [0, 1] |
+------------+-----------------------+---------------+
- This implementation uses the equations given in
:cite:`Sarifuddin2005a`.
- This implementation used the equations given in
:cite:`Sarifuddin2005a` and the corrections from
:cite:`Sarifuddin2021`.
References
----------
Expand All @@ -416,9 +420,7 @@ def RGB_to_HCL(
Max = np.maximum(np.maximum(R, G), B)

with sdiv_mode():
alpha = sdiv(Min, Max) / Y_0

Q = np.exp(alpha * gamma)
Q = np.exp(sdiv(Min * gamma, Max * Y_0))

L = (Q * Max + (Q - 1) * Min) / 2

Expand All @@ -428,24 +430,26 @@ def RGB_to_HCL(

C = Q * (np.abs(R_G) + np.abs(G_B) + np.abs(B_R)) / 3

with sdiv_mode():
with sdiv_mode("Ignore"):
H = np.arctan(sdiv(G_B, R_G))

_2_3_H = 2 / 3 * H
_4_3_H = 4 / 3 * H
_2_H_3 = 2 * H / 3
_4_H_3 = 4 * H / 3

H = np.select(
[
C == 0,
np.logical_and(R_G >= 0, G_B >= 0),
np.logical_and(R_G >= 0, G_B < 0),
np.logical_and(R_G < 0, G_B >= 0),
np.logical_and(R_G < 0, G_B < 0),
],
[
_2_3_H,
_4_3_H,
np.pi + _4_3_H,
_2_3_H - np.pi,
0,
_2_H_3,
_4_H_3,
np.pi + _4_H_3,
_2_H_3 - np.pi,
],
)

Expand Down Expand Up @@ -489,8 +493,9 @@ def HCL_to_RGB(
| ``RGB`` | [0, 1] | [0, 1] |
+------------+-----------------------+---------------+
- This implementation uses the equations given in
:cite:`Sarifuddin2005a`.
- This implementation used the equations given in
:cite:`Sarifuddin2005a` and the corrections from
:cite:`Sarifuddin2021`.
References
----------
Expand All @@ -511,11 +516,6 @@ def HCL_to_RGB(
Min = sdiv(4 * L - 3 * C, 4 * Q - 2)
Max = Min + sdiv(3 * C, 2 * Q)

def _1_2_3(a: ArrayLike) -> NDArrayFloat:
"""Tail-stack given :math:`a` array as a *bool* dtype."""

return tstack(cast(ArrayLike, [a, a, a]), dtype=np.bool_)

tan_3_2_H = np.tan(3 / 2 * H)
tan_3_4_H_MP = np.tan(3 / 4 * (H - np.pi))
tan_3_4_H = np.tan(3 / 4 * H)
Expand All @@ -526,6 +526,11 @@ def _1_2_3(a: ArrayLike) -> NDArrayFloat:
r_n60 = np.radians(-60)
r_n120 = np.radians(-120)

def _1_2_3(a: ArrayLike) -> NDArrayFloat:
"""Tail-stack given :math:`a` array as a *bool* dtype."""

return tstack(cast(ArrayLike, [a, a, a]), dtype=np.bool_)

with sdiv_mode():
RGB = np.select(
[
Expand Down

0 comments on commit 1a580ef

Please sign in to comment.