• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

colour-science / colour / 12452776384

22 Dec 2024 08:43AM UTC coverage: 97.524% (-1.7%) from 99.255%
12452776384

push

github

KelSolaar
Code formatting.

41247 of 42294 relevant lines covered (97.52%)

0.98 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.44
/colour/models/rgb/transfer_functions/common.py
1
"""
2
Common Transfer Functions Utilities
3
===================================
4

5
Define various transfer functions common utilities.
6
"""
7

8
from __future__ import annotations
1✔
9

10
import typing
1✔
11

12
import numpy as np
1✔
13

14
if typing.TYPE_CHECKING:
1✔
15
    from colour.hints import ArrayLike, NDArrayReal
×
16

17
from colour.utilities import as_float, as_float_array, as_int, as_int_array
1✔
18

19
__author__ = "Colour Developers"
1✔
20
__copyright__ = "Copyright 2013 Colour Developers"
1✔
21
__license__ = "BSD-3-Clause - https://opensource.org/licenses/BSD-3-Clause"
1✔
22
__maintainer__ = "Colour Developers"
1✔
23
__email__ = "colour-developers@colour-science.org"
1✔
24
__status__ = "Production"
1✔
25

26
__all__ = [
1✔
27
    "CV_range",
28
    "legal_to_full",
29
    "full_to_legal",
30
]
31

32

33
def CV_range(
1✔
34
    bit_depth: int = 10, is_legal: bool = False, is_int: bool = False
35
) -> NDArrayReal:
36
    """
37
    Return the code value :math:`CV` range for given bit-depth, range legality
38
    and representation.
39

40
    Parameters
41
    ----------
42
    bit_depth
43
        Bit-depth of the code value :math:`CV` range.
44
    is_legal
45
        Whether the code value :math:`CV` range is legal.
46
    is_int
47
        Whether the code value :math:`CV` range represents int code values.
48

49
    Returns
50
    -------
51
    :class:`numpy.ndarray`
52
        Code value :math:`CV` range.
53

54
    Examples
55
    --------
56
    >>> CV_range(8, True, True)
57
    array([ 16, 235])
58
    >>> CV_range(8, True, False)  # doctest: +ELLIPSIS
59
    array([ 0.0627451...,  0.9215686...])
60
    >>> CV_range(10, False, False)
61
    array([ 0.,  1.])
62
    """
63

64
    if is_legal:
1✔
65
        ranges = np.array([16, 235])
1✔
66
        ranges *= 2 ** (bit_depth - 8)
1✔
67
    else:
68
        ranges = np.array([0, 2**bit_depth - 1])
1✔
69

70
    if not is_int:
1✔
71
        ranges = as_float_array(ranges) / (2**bit_depth - 1)
1✔
72

73
    return ranges
1✔
74

75

76
def legal_to_full(
1✔
77
    CV: ArrayLike,
78
    bit_depth: int = 10,
79
    in_int: bool = False,
80
    out_int: bool = False,
81
) -> NDArrayReal:
82
    """
83
    Convert given code value :math:`CV` or float equivalent of a code value at
84
    a given bit-depth from legal range (studio swing) to full range
85
    (full swing).
86

87
    Parameters
88
    ----------
89
    CV
90
        Legal range code value :math:`CV` or float equivalent of a code value
91
        at a given bit-depth.
92
    bit_depth
93
        Bit-depth used for conversion.
94
    in_int
95
        Whether to treat the input value as int code value or float
96
        equivalent of a code value at a given bit-depth.
97
    out_int
98
        Whether to return value as int code value or float equivalent of a
99
        code value at a given bit-depth.
100

101
    Returns
102
    -------
103
    :class:`numpy.ndarray`
104
        Full range code value :math:`CV` or float equivalent of a code value
105
        at a given bit-depth.
106

107
    Examples
108
    --------
109
    >>> legal_to_full(64 / 1023)
110
    0.0
111
    >>> legal_to_full(940 / 1023)
112
    1.0
113
    >>> legal_to_full(64 / 1023, out_int=True)
114
    0
115
    >>> legal_to_full(940 / 1023, out_int=True)
116
    1023
117
    >>> legal_to_full(64, in_int=True)
118
    0.0
119
    >>> legal_to_full(940, in_int=True)
120
    1.0
121
    >>> legal_to_full(64, in_int=True, out_int=True)
122
    0
123
    >>> legal_to_full(940, in_int=True, out_int=True)
124
    1023
125
    """
126

127
    CV = as_float_array(CV)
1✔
128

129
    MV = 2**bit_depth - 1
1✔
130

131
    CV_full = as_int_array(np.round(CV)) if in_int else CV * MV
1✔
132

133
    B, W = CV_range(bit_depth, True, True)
1✔
134

135
    CV_full = (CV_full - B) / (W - B)
1✔
136

137
    if out_int:
1✔
138
        return as_int(np.round(CV_full * MV))
1✔
139

140
    return as_float(CV_full)
1✔
141

142

143
def full_to_legal(
1✔
144
    CV: ArrayLike,
145
    bit_depth: int = 10,
146
    in_int: bool = False,
147
    out_int: bool = False,
148
) -> NDArrayReal:
149
    """
150
    Convert given code value :math:`CV` or float equivalent of a code value at
151
    a given bit-depth from full range (full swing) to legal range
152
    (studio swing).
153

154
    Parameters
155
    ----------
156
    CV
157
        Full range code value :math:`CV` or float equivalent of a code value at
158
        a given bit-depth.
159
    bit_depth
160
        Bit-depth used for conversion.
161
    in_int
162
        Whether to treat the input value as int code value or float
163
        equivalent of a code value at a given bit-depth.
164
    out_int
165
        Whether to return value as int code value or float equivalent of a
166
        code value at a given bit-depth.
167

168
    Returns
169
    -------
170
    :class:`numpy.ndarray`
171
        Legal range code value :math:`CV` or float equivalent of a code value
172
        at a given bit-depth.
173

174
    Examples
175
    --------
176
    >>> full_to_legal(0.0)  # doctest: +ELLIPSIS
177
    0.0625610...
178
    >>> full_to_legal(1.0)  # doctest: +ELLIPSIS
179
    0.9188660...
180
    >>> full_to_legal(0.0, out_int=True)
181
    64
182
    >>> full_to_legal(1.0, out_int=True)
183
    940
184
    >>> full_to_legal(0, in_int=True)  # doctest: +ELLIPSIS
185
    0.0625610...
186
    >>> full_to_legal(1023, in_int=True)  # doctest: +ELLIPSIS
187
    0.9188660...
188
    >>> full_to_legal(0, in_int=True, out_int=True)
189
    64
190
    >>> full_to_legal(1023, in_int=True, out_int=True)
191
    940
192
    """
193

194
    CV = as_float_array(CV)
1✔
195

196
    MV = 2**bit_depth - 1
1✔
197

198
    CV_legal = as_int_array(np.round(CV / MV)) if in_int else CV
1✔
199

200
    B, W = CV_range(bit_depth, True, True)
1✔
201

202
    CV_legal = (W - B) * CV_legal + B
1✔
203

204
    if out_int:
1✔
205
        return as_int(np.round(CV_legal))
1✔
206

207
    return as_float(CV_legal / MV)
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc