Coverage for models/rgb/cmyk.py: 56%

34 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-11-16 22:49 +1300

1""" 

2CMYK Colour Transformations 

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

4 

5Define various Cyan-Magenta-Yellow (Black) (CMY(K)) colour transformations: 

6 

7- :func:`colour.RGB_to_CMY` 

8- :func:`colour.CMY_to_RGB` 

9- :func:`colour.CMY_to_CMYK` 

10- :func:`colour.CMYK_to_CMY` 

11 

12References 

13---------- 

14- :cite:`EasyRGBh` : EasyRGB. (n.d.). RGB --> CMY. Retrieved May 18, 2014, 

15 from http://www.easyrgb.com/index.php?X=MATH&H=11#text11 

16- :cite:`EasyRGBi` : EasyRGB. (n.d.). CMY --> RGB. Retrieved May 18, 2014, 

17 from http://www.easyrgb.com/index.php?X=MATH&H=12#text12 

18- :cite:`EasyRGBm` : EasyRGB. (n.d.). CMYK --> CMY. Retrieved May 18, 2014, 

19 from http://www.easyrgb.com/index.php?X=MATH&H=14#text14 

20- :cite:`EasyRGBo` : EasyRGB. (n.d.). CMY --> CMYK. Retrieved May 18, 2014, 

21 from http://www.easyrgb.com/index.php?X=MATH&H=13#text13 

22""" 

23 

24from __future__ import annotations 

25 

26import numpy as np 

27 

28from colour.hints import ( # noqa: TC001 

29 Domain1, 

30 Range1, 

31) 

32from colour.utilities import as_float_array, from_range_1, to_domain_1, tsplit, tstack 

33 

34__author__ = "Colour Developers" 

35__copyright__ = "Copyright 2013 Colour Developers" 

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

37__maintainer__ = "Colour Developers" 

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

39__status__ = "Production" 

40 

41__all__ = [ 

42 "RGB_to_CMY", 

43 "CMY_to_RGB", 

44 "CMY_to_CMYK", 

45 "CMYK_to_CMY", 

46] 

47 

48 

49def RGB_to_CMY(RGB: Domain1) -> Range1: 

50 """ 

51 Convert from *RGB* colourspace to *CMY* colourspace. 

52 

53 Parameters 

54 ---------- 

55 RGB 

56 *RGB* colourspace array. 

57 

58 Returns 

59 ------- 

60 :class:`numpy.ndarray` 

61 *CMY* array. 

62 

63 Notes 

64 ----- 

65 +------------+-----------------------+---------------+ 

66 | **Domain** | **Scale - Reference** | **Scale - 1** | 

67 +============+=======================+===============+ 

68 | ``RGB`` | 1 | 1 | 

69 +------------+-----------------------+---------------+ 

70 

71 +------------+-----------------------+---------------+ 

72 | **Range** | **Scale - Reference** | **Scale - 1** | 

73 +============+=======================+===============+ 

74 | ``CMY`` | 1 | 1 | 

75 +------------+-----------------------+---------------+ 

76 

77 References 

78 ---------- 

79 :cite:`EasyRGBh` 

80 

81 Examples 

82 -------- 

83 >>> RGB = np.array([0.45620519, 0.03081071, 0.04091952]) 

84 >>> RGB_to_CMY(RGB) # doctest: +ELLIPSIS 

85 array([ 0.5437948..., 0.9691892..., 0.9590804...]) 

86 """ 

87 

88 CMY = 1 - to_domain_1(RGB) 

89 

90 return from_range_1(CMY) 

91 

92 

93def CMY_to_RGB(CMY: Domain1) -> Range1: 

94 """ 

95 Convert from *CMY* colourspace to *RGB* colourspace. 

96 

97 Parameters 

98 ---------- 

99 CMY 

100 *CMY* colourspace array. 

101 

102 Returns 

103 ------- 

104 :class:`numpy.ndarray` 

105 *RGB* colourspace array. 

106 

107 Notes 

108 ----- 

109 +------------+-----------------------+---------------+ 

110 | **Domain** | **Scale - Reference** | **Scale - 1** | 

111 +============+=======================+===============+ 

112 | ``CMY`` | 1 | 1 | 

113 +------------+-----------------------+---------------+ 

114 

115 +------------+-----------------------+---------------+ 

116 | **Range** | **Scale - Reference** | **Scale - 1** | 

117 +============+=======================+===============+ 

118 | ``RGB`` | 1 | 1 | 

119 +------------+-----------------------+---------------+ 

120 

121 References 

122 ---------- 

123 :cite:`EasyRGBi` 

124 

125 Examples 

126 -------- 

127 >>> CMY = np.array([0.54379481, 0.96918929, 0.95908048]) 

128 >>> CMY_to_RGB(CMY) # doctest: +ELLIPSIS 

129 array([ 0.4562051..., 0.0308107..., 0.0409195...]) 

130 """ 

131 

132 RGB = 1 - to_domain_1(CMY) 

133 

134 return from_range_1(RGB) 

135 

136 

137def CMY_to_CMYK(CMY: Domain1) -> Range1: 

138 """ 

139 Convert from *CMY* colourspace to *CMYK* colourspace. 

140 

141 Parameters 

142 ---------- 

143 CMY 

144 *CMY* colourspace array. 

145 

146 Returns 

147 ------- 

148 :class:`numpy.ndarray` 

149 *CMYK* array. 

150 

151 Notes 

152 ----- 

153 +------------+-----------------------+---------------+ 

154 | **Domain** | **Scale - Reference** | **Scale - 1** | 

155 +============+=======================+===============+ 

156 | ``CMY`` | 1 | 1 | 

157 +------------+-----------------------+---------------+ 

158 

159 +------------+-----------------------+---------------+ 

160 | **Range** | **Scale - Reference** | **Scale - 1** | 

161 +============+=======================+===============+ 

162 | ``CMYK`` | 1 | 1 | 

163 +------------+-----------------------+---------------+ 

164 

165 References 

166 ---------- 

167 :cite:`EasyRGBo` 

168 

169 Examples 

170 -------- 

171 >>> CMY = np.array([0.54379481, 0.96918929, 0.95908048]) 

172 >>> CMY_to_CMYK(CMY) # doctest: +ELLIPSIS 

173 array([ 0. , 0.9324630..., 0.9103045..., 0.5437948...]) 

174 """ 

175 

176 C, M, Y = tsplit(to_domain_1(CMY)) 

177 

178 K = np.where(C < 1, C, 1) 

179 K = np.where(M < K, M, K) 

180 K = np.where(Y < K, Y, K) 

181 

182 C = as_float_array((C - K) / (1 - K)) 

183 M = as_float_array((M - K) / (1 - K)) 

184 Y = as_float_array((Y - K) / (1 - K)) 

185 

186 C[np.asarray(K == 1)] = 0 

187 M[np.asarray(K == 1)] = 0 

188 Y[np.asarray(K == 1)] = 0 

189 

190 CMYK = tstack([C, M, Y, K]) 

191 

192 return from_range_1(CMYK) 

193 

194 

195def CMYK_to_CMY(CMYK: Domain1) -> Range1: 

196 """ 

197 Convert from *CMYK* colourspace to *CMY* colourspace. 

198 

199 Parameters 

200 ---------- 

201 CMYK 

202 *CMYK* colourspace array. 

203 

204 Returns 

205 ------- 

206 :class:`numpy.ndarray` 

207 *CMY* array. 

208 

209 Notes 

210 ----- 

211 +------------+-----------------------+---------------+ 

212 | **Domain** | **Scale - Reference** | **Scale - 1** | 

213 +============+=======================+===============+ 

214 | ``CMYK`` | 1 | 1 | 

215 +------------+-----------------------+---------------+ 

216 

217 +------------+-----------------------+---------------+ 

218 | **Range** | **Scale - Reference** | **Scale - 1** | 

219 +============+=======================+===============+ 

220 | ``CMY`` | 1 | 1 | 

221 +------------+-----------------------+---------------+ 

222 

223 References 

224 ---------- 

225 :cite:`EasyRGBm` 

226 

227 Examples 

228 -------- 

229 >>> CMYK = np.array([0.50000000, 0.00000000, 0.74400000, 0.01960784]) 

230 >>> CMYK_to_CMY(CMYK) # doctest: +ELLIPSIS 

231 array([ 0.5098039..., 0.0196078..., 0.7490196...]) 

232 """ 

233 

234 C, M, Y, K = tsplit(to_domain_1(CMYK)) 

235 

236 CMY = tstack([C * (1 - K) + K, M * (1 - K) + K, Y * (1 - K) + K]) 

237 

238 return from_range_1(CMY)