Skip to content

Add ColorConfig::getColorSpaceIndex -- look up index by name #3758

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/include/OpenImageIO/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ class OIIO_API ColorConfig {
/// Query the name of the specified ColorSpace.
const char* getColorSpaceNameByIndex(int index) const;

/// Given a color space name, return the index of an equivalent color
/// space, or -1 if not found. It will first look for an exact match of
/// the name, but if not found, will match a color space that is
/// "equivalent" to the named color space.
int getColorSpaceIndex(string_view name) const;

/// Get the name of the color space representing the named role,
/// or NULL if none could be identified.
const char* getColorSpaceNameByRole(string_view role) const;
Expand Down
16 changes: 16 additions & 0 deletions src/libOpenImageIO/color_ocio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,22 @@ ColorConfig::getColorSpaceNameByIndex(int index) const



int
ColorConfig::getColorSpaceIndex(string_view name) const
{
// Check for exact matches
for (int i = 0, e = (int)m_impl->colorspaces.size(); i < e; ++i)
if (Strutil::iequals(m_impl->colorspaces[i].name, name))
return i;
// Check for aliases and equivalents
for (int i = 0, e = (int)m_impl->colorspaces.size(); i < e; ++i)
if (equivalent(m_impl->colorspaces[i].name, name))
return i;
return -1;
}



const char*
ColorConfig::getColorSpaceFamilyByName(string_view name) const
{
Expand Down
6 changes: 6 additions & 0 deletions src/python/py_colorconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ declare_colorconfig(py::module& m)
.def("getNumColorSpaces", &ColorConfig::getNumColorSpaces)
.def("getColorSpaceNames", &ColorConfig::getColorSpaceNames)
.def("getColorSpaceNameByIndex", &ColorConfig::getColorSpaceNameByIndex)
.def(
"getColorSpaceIndex",
[](const ColorConfig& self, const std::string& name) {
return self.getColorSpaceIndex(name);
},
"name"_a)
.def(
"getColorSpaceNameByRole",
[](const ColorConfig& self, const std::string& role) {
Expand Down
3 changes: 3 additions & 0 deletions testsuite/python-colorconfig/ref/out-noocio-python27.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
getNumColorSpaces = 8
getColorSpaceNames = [u'linear', u'scene_linear', u'default', u'rgb', u'RGB', u'lin_srgb', u'sRGB', u'Rec709']
Index of 'lin_srgb' = 5
Index of 'unknown' = -1
Name of color space 2 = default
getNumLooks = 0
getLookNames = []
getNumDisplays = 0
Expand Down
3 changes: 3 additions & 0 deletions testsuite/python-colorconfig/ref/out-noocio-python3.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
getNumColorSpaces = 8
getColorSpaceNames = ['linear', 'scene_linear', 'default', 'rgb', 'RGB', 'lin_srgb', 'sRGB', 'Rec709']
Index of 'lin_srgb' = 5
Index of 'unknown' = -1
Name of color space 2 = default
getNumLooks = 0
getLookNames = []
getNumDisplays = 0
Expand Down
3 changes: 3 additions & 0 deletions testsuite/python-colorconfig/ref/out-ocio11-python27.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
getNumColorSpaces = 14
getColorSpaceNames = [u'linear', u'sRGB', u'sRGBf', u'rec709', u'Cineon', u'Gamma1.8', u'Gamma2.2', u'Panalog', u'REDLog', u'ViperLog', u'AlexaV3LogC', u'PLogLin', u'SLog', u'raw']
Index of 'lin_srgb' = 0
Index of 'unknown' = -1
Name of color space 2 = sRGBf
getNumLooks = 0
getLookNames = []
getNumDisplays = 1
Expand Down
3 changes: 3 additions & 0 deletions testsuite/python-colorconfig/ref/out-ocio11-python3.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
getNumColorSpaces = 14
getColorSpaceNames = ['linear', 'sRGB', 'sRGBf', 'rec709', 'Cineon', 'Gamma1.8', 'Gamma2.2', 'Panalog', 'REDLog', 'ViperLog', 'AlexaV3LogC', 'PLogLin', 'SLog', 'raw']
Index of 'lin_srgb' = 0
Index of 'unknown' = -1
Name of color space 2 = sRGBf
getNumLooks = 0
getLookNames = []
getNumDisplays = 1
Expand Down
3 changes: 3 additions & 0 deletions testsuite/python-colorconfig/ref/out-ocio21-python27-alt.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
getNumColorSpaces = 14
getColorSpaceNames = [u'linear', u'sRGB', u'sRGBf', u'rec709', u'Cineon', u'Gamma1.8', u'Gamma2.2', u'Panalog', u'REDLog', u'ViperLog', u'AlexaV3LogC', u'PLogLin', u'SLog', u'raw']
Index of 'lin_srgb' = 0
Index of 'unknown' = -1
Name of color space 2 = sRGBf
getNumLooks = 0
getLookNames = []
getNumDisplays = 1
Expand Down
3 changes: 3 additions & 0 deletions testsuite/python-colorconfig/ref/out-ocio21-python27.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
getNumColorSpaces = 14
getColorSpaceNames = [u'linear', u'sRGB', u'sRGBf', u'rec709', u'Cineon', u'Gamma1.8', u'Gamma2.2', u'Panalog', u'REDLog', u'ViperLog', u'AlexaV3LogC', u'PLogLin', u'SLog', u'raw']
Index of 'lin_srgb' = 0
Index of 'unknown' = -1
Name of color space 2 = sRGBf
getNumLooks = 0
getLookNames = []
getNumDisplays = 1
Expand Down
3 changes: 3 additions & 0 deletions testsuite/python-colorconfig/ref/out-ocio21-python3.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
getNumColorSpaces = 14
getColorSpaceNames = ['linear', 'sRGB', 'sRGBf', 'rec709', 'Cineon', 'Gamma1.8', 'Gamma2.2', 'Panalog', 'REDLog', 'ViperLog', 'AlexaV3LogC', 'PLogLin', 'SLog', 'raw']
Index of 'lin_srgb' = 0
Index of 'unknown' = -1
Name of color space 2 = sRGBf
getNumLooks = 0
getLookNames = []
getNumDisplays = 1
Expand Down
3 changes: 3 additions & 0 deletions testsuite/python-colorconfig/ref/out-ocio22.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
getNumColorSpaces = 14
getColorSpaceNames = ['ACES2065-1', 'ACEScc', 'ACEScct', 'ACEScg', 'Linear P3-D65', 'Linear Rec.2020', 'Linear Rec.709 (sRGB)', 'Gamma 1.8 Rec.709 - Texture', 'Gamma 2.2 AP1 - Texture', 'Gamma 2.2 Rec.709 - Texture', 'Gamma 2.4 Rec.709 - Texture', 'sRGB Encoded AP1 - Texture', 'sRGB - Texture', 'Raw']
Index of 'lin_srgb' = 6
Index of 'unknown' = -1
Name of color space 2 = ACEScct
getNumLooks = 1
getLookNames = ['ACES 1.3 Reference Gamut Compression']
getNumDisplays = 5
Expand Down
4 changes: 3 additions & 1 deletion testsuite/python-colorconfig/src/test_colorconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
config = oiio.ColorConfig()
print ("getNumColorSpaces =", config.getNumColorSpaces())
print ("getColorSpaceNames =", config.getColorSpaceNames())

print ("Index of 'lin_srgb' =", config.getColorSpaceIndex('lin_srgb'))
print ("Index of 'unknown' =", config.getColorSpaceIndex('unknown'))
print ("Name of color space 2 =", config.getColorSpaceNameByIndex(2))
print ("getNumLooks =", config.getNumLooks())
print ("getLookNames =", config.getLookNames())

Expand Down