Coverage for models/rgb/ycocg.py: 11%

1"""

2 YCoCg Colour Encoding

3=====================

4

5 Define the *YCoCg* colour encoding related transformations:

6

7- :func:`colour.RGB_to_YCoCg`

8- :func:`colour.YCoCg_to_RGB`

9

10 References

11----------

12- :cite:`Malvar2003` : Malvar, H., & Sullivan, G. (2003). YCoCg-R: A Color

13 Space with RGB Reversibility and Low Dynamic Range.

14 https://www.microsoft.com/en-us/research/wp-content/uploads/2016/06/\

15 Malvar_Sullivan_YCoCg-R_JVT-I014r3-2.pdf

16"""

17

18 from __future__ import annotations

19

20 import typing

21

22 import numpy as np

23

24 from colour.algebra import vecmul

25

26 if typing.TYPE_CHECKING:

27 from colour.hints import ArrayLike, NDArrayFloat

28

29 __author__ = "Colour Developers"

30 __copyright__ = "Copyright 2013 Colour Developers"

31 __license__ = "BSD-3-Clause - https://opensource.org/licenses/BSD-3-Clause"

32 __maintainer__ = "Colour Developers"

33 __email__ = "colour-developers@colour-science.org"

34 __status__ = "Development"

35

36 __all__ = [

37 "MATRIX_RGB_TO_YCOCG",

38 "MATRIX_YCOCG_TO_RGB",

39 "RGB_to_YCoCg",

40 "YCoCg_to_RGB",

41]

42

43 MATRIX_RGB_TO_YCOCG: NDArrayFloat = np.array(

44 [

45 [1 / 4, 1 / 2, 1 / 4],

46 [1 / 2, 0, -1 / 2],

47 [-1 / 4, 1 / 2, -1 / 4],

48 ]

49)

50"""*R'G'B'* colourspace to *YCoCg* colour encoding matrix."""

51

52 MATRIX_YCOCG_TO_RGB: NDArrayFloat = np.array(

53 [

54 [1, 1, -1],

55 [1, 0, 1],

56 [1, -1, -1],

57 ]

58)

59"""*YCoCg* colour encoding to *R'G'B'* colourspace matrix."""

60

61

62 def RGB_to_YCoCg(RGB: ArrayLike) -> NDArrayFloat:

63 """

64 Convert an array of *R'G'B'* values to the corresponding *YCoCg* colour

65 encoding values array.

66

67 Parameters

68 ----------

69 RGB

70 Input *R'G'B'* array.

71

72 Returns

73 -------

74 :class:`numpy.ndarray`

75 *YCoCg* colour encoding array.

76

77 References

78 ----------

79 :cite:`Malvar2003`

80

81 Examples

82 --------

83 >>> RGB_to_YCoCg(np.array([1.0, 1.0, 1.0]))

84 array([ 1., 0., 0.])

85 >>> RGB_to_YCoCg(np.array([0.75, 0.5, 0.5]))

86 array([ 0.5625, 0.125 , -0.0625])

87 """

88

89 return vecmul(MATRIX_RGB_TO_YCOCG, RGB)

90

91

92 def YCoCg_to_RGB(YCoCg: ArrayLike) -> NDArrayFloat:

93 """

94 Convert an array of *YCoCg* colour encoding values to the corresponding

95 *R'G'B'* values array.

96

97 Parameters

98 ----------

99 YCoCg

100 *YCoCg* colour encoding array.

101

102 Returns

103 -------

104 :class:`numpy.ndarray`

105 Output *R'G'B'* array.

106

107 References

108 ----------

109 :cite:`Malvar2003`

110

111 Examples

112 --------

113 >>> YCoCg_to_RGB(np.array([1.0, 0.0, 0.0]))

114 array([ 1., 1., 1.])

115 >>> YCoCg_to_RGB(np.array([0.5625, 0.125, -0.0625]))

116 array([ 0.75, 0.5 , 0.5 ])

117 """

118

119 return vecmul(MATRIX_YCOCG_TO_RGB, YCoCg)