diff --git a/src/include/OpenImageIO/color.h b/src/include/OpenImageIO/color.h index a88897bacf..eaeb4f8bb7 100644 --- a/src/include/OpenImageIO/color.h +++ b/src/include/OpenImageIO/color.h @@ -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; diff --git a/src/libOpenImageIO/color_ocio.cpp b/src/libOpenImageIO/color_ocio.cpp index 77321eb343..5c409cc687 100644 --- a/src/libOpenImageIO/color_ocio.cpp +++ b/src/libOpenImageIO/color_ocio.cpp @@ -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 { diff --git a/src/python/py_colorconfig.cpp b/src/python/py_colorconfig.cpp index 91bf36da39..5ade7dbd82 100644 --- a/src/python/py_colorconfig.cpp +++ b/src/python/py_colorconfig.cpp @@ -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) { diff --git a/testsuite/python-colorconfig/ref/out-noocio-python27.txt b/testsuite/python-colorconfig/ref/out-noocio-python27.txt index 5f2541a43a..6c27481bcd 100644 --- a/testsuite/python-colorconfig/ref/out-noocio-python27.txt +++ b/testsuite/python-colorconfig/ref/out-noocio-python27.txt @@ -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 diff --git a/testsuite/python-colorconfig/ref/out-noocio-python3.txt b/testsuite/python-colorconfig/ref/out-noocio-python3.txt index 824f17337b..1c9dda6de6 100644 --- a/testsuite/python-colorconfig/ref/out-noocio-python3.txt +++ b/testsuite/python-colorconfig/ref/out-noocio-python3.txt @@ -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 diff --git a/testsuite/python-colorconfig/ref/out-ocio11-python27.txt b/testsuite/python-colorconfig/ref/out-ocio11-python27.txt index f0c05c3a10..ae0e22622b 100644 --- a/testsuite/python-colorconfig/ref/out-ocio11-python27.txt +++ b/testsuite/python-colorconfig/ref/out-ocio11-python27.txt @@ -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 diff --git a/testsuite/python-colorconfig/ref/out-ocio11-python3.txt b/testsuite/python-colorconfig/ref/out-ocio11-python3.txt index 571c02a47a..83e3b4771f 100644 --- a/testsuite/python-colorconfig/ref/out-ocio11-python3.txt +++ b/testsuite/python-colorconfig/ref/out-ocio11-python3.txt @@ -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 diff --git a/testsuite/python-colorconfig/ref/out-ocio21-python27-alt.txt b/testsuite/python-colorconfig/ref/out-ocio21-python27-alt.txt index e36a8c6a1c..499fb5c801 100644 --- a/testsuite/python-colorconfig/ref/out-ocio21-python27-alt.txt +++ b/testsuite/python-colorconfig/ref/out-ocio21-python27-alt.txt @@ -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 diff --git a/testsuite/python-colorconfig/ref/out-ocio21-python27.txt b/testsuite/python-colorconfig/ref/out-ocio21-python27.txt index 9f1b1bf370..1f72fafa65 100644 --- a/testsuite/python-colorconfig/ref/out-ocio21-python27.txt +++ b/testsuite/python-colorconfig/ref/out-ocio21-python27.txt @@ -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 diff --git a/testsuite/python-colorconfig/ref/out-ocio21-python3.txt b/testsuite/python-colorconfig/ref/out-ocio21-python3.txt index 67e6811b63..9335653915 100644 --- a/testsuite/python-colorconfig/ref/out-ocio21-python3.txt +++ b/testsuite/python-colorconfig/ref/out-ocio21-python3.txt @@ -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 diff --git a/testsuite/python-colorconfig/ref/out-ocio22.txt b/testsuite/python-colorconfig/ref/out-ocio22.txt index 3037976b2d..1f9c18c8c7 100644 --- a/testsuite/python-colorconfig/ref/out-ocio22.txt +++ b/testsuite/python-colorconfig/ref/out-ocio22.txt @@ -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 diff --git a/testsuite/python-colorconfig/src/test_colorconfig.py b/testsuite/python-colorconfig/src/test_colorconfig.py index 6f94a501d6..9712e1d88b 100755 --- a/testsuite/python-colorconfig/src/test_colorconfig.py +++ b/testsuite/python-colorconfig/src/test_colorconfig.py @@ -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())