[Windows] Fix driver version detection of AMD RDNA+ GPU on Windows 10
[xbmc.git] / xbmc / guilib / GUIColorManager.cpp
blob0034fedce007cf55d3b38cb61aa82b4aed7dc6ba
1 /*
2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
9 #include "GUIColorManager.h"
11 #include "addons/Skin.h"
12 #include "filesystem/SpecialProtocol.h"
13 #include "utils/ColorUtils.h"
14 #include "utils/StringUtils.h"
15 #include "utils/URIUtils.h"
16 #include "utils/XBMCTinyXML.h"
17 #include "utils/log.h"
19 CGUIColorManager::CGUIColorManager(void) = default;
21 CGUIColorManager::~CGUIColorManager(void)
23 Clear();
26 void CGUIColorManager::Clear()
28 m_colors.clear();
31 // load the color file in
32 void CGUIColorManager::Load(const std::string &colorFile)
34 Clear();
36 // load the global color map if it exists
37 CXBMCTinyXML xmlDoc;
38 if (xmlDoc.LoadFile(CSpecialProtocol::TranslatePathConvertCase("special://xbmc/system/colors.xml")))
39 LoadXML(xmlDoc);
41 // first load the default color map if it exists
42 std::string path = URIUtils::AddFileToFolder(g_SkinInfo->Path(), "colors", "defaults.xml");
44 if (xmlDoc.LoadFile(CSpecialProtocol::TranslatePathConvertCase(path)))
45 LoadXML(xmlDoc);
47 // now the color map requested
48 if (StringUtils::EqualsNoCase(colorFile, "SKINDEFAULT"))
49 return; // nothing to do
51 path = URIUtils::AddFileToFolder(g_SkinInfo->Path(), "colors", colorFile);
52 if (!URIUtils::HasExtension(path))
53 path += ".xml";
54 CLog::Log(LOGINFO, "Loading colors from {}", path);
56 if (xmlDoc.LoadFile(path))
57 LoadXML(xmlDoc);
60 bool CGUIColorManager::LoadXML(CXBMCTinyXML &xmlDoc)
62 TiXmlElement* pRootElement = xmlDoc.RootElement();
64 std::string strValue = pRootElement->Value();
65 if (strValue != std::string("colors"))
67 CLog::Log(LOGERROR, "color file doesn't start with <colors>");
68 return false;
71 const TiXmlElement *color = pRootElement->FirstChildElement("color");
73 while (color)
75 if (color->FirstChild() && color->Attribute("name"))
77 UTILS::COLOR::Color value = 0xffffffff;
78 sscanf(color->FirstChild()->Value(), "%x", (unsigned int*) &value);
79 std::string name = color->Attribute("name");
80 const auto it = m_colors.find(name);
81 if (it != m_colors.end())
82 (*it).second = value;
83 else
84 m_colors.insert(make_pair(name, value));
86 color = color->NextSiblingElement("color");
88 return true;
91 // lookup a color and return it's hex value
92 UTILS::COLOR::Color CGUIColorManager::GetColor(const std::string& color) const
94 // look in our color map
95 std::string trimmed(color);
96 StringUtils::TrimLeft(trimmed, "= ");
97 const auto it = m_colors.find(trimmed);
98 if (it != m_colors.end())
99 return (*it).second;
101 // try converting hex directly
102 UTILS::COLOR::Color value = 0;
103 sscanf(trimmed.c_str(), "%x", &value);
104 return value;
107 bool CGUIColorManager::LoadColorsListFromXML(
108 const std::string& filePath,
109 std::vector<std::pair<std::string, UTILS::COLOR::ColorInfo>>& colors,
110 bool sortColors)
112 CLog::Log(LOGDEBUG, "Loading colors from file {}", filePath);
113 CXBMCTinyXML xmlDoc;
114 if (!xmlDoc.LoadFile(filePath))
116 CLog::Log(LOGERROR, "{} - Failed to load colors from file {}", __FUNCTION__, filePath);
117 return false;
120 TiXmlElement* pRootElement = xmlDoc.RootElement();
121 std::string strValue = pRootElement->Value();
122 if (strValue != std::string("colors"))
124 CLog::Log(LOGERROR, "{} - Color file doesn't start with <colors>", __FUNCTION__);
125 return false;
128 const TiXmlElement* xmlColor = pRootElement->FirstChildElement("color");
129 while (xmlColor)
131 if (xmlColor->FirstChild() && xmlColor->Attribute("name"))
133 colors.emplace_back(xmlColor->Attribute("name"),
134 UTILS::COLOR::MakeColorInfo(xmlColor->FirstChild()->Value()));
136 xmlColor = xmlColor->NextSiblingElement("color");
139 if (sortColors)
140 std::sort(colors.begin(), colors.end(), UTILS::COLOR::comparePairColorInfo);
142 return true;