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.
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)
26 void CGUIColorManager::Clear()
31 // load the color file in
32 void CGUIColorManager::Load(const std::string
&colorFile
)
36 // load the global color map if it exists
38 if (xmlDoc
.LoadFile(CSpecialProtocol::TranslatePathConvertCase("special://xbmc/system/colors.xml")))
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
)))
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
))
54 CLog::Log(LOGINFO
, "Loading colors from {}", path
);
56 if (xmlDoc
.LoadFile(path
))
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>");
71 const TiXmlElement
*color
= pRootElement
->FirstChildElement("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())
84 m_colors
.insert(make_pair(name
, value
));
86 color
= color
->NextSiblingElement("color");
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())
101 // try converting hex directly
102 UTILS::COLOR::Color value
= 0;
103 sscanf(trimmed
.c_str(), "%x", &value
);
107 bool CGUIColorManager::LoadColorsListFromXML(
108 const std::string
& filePath
,
109 std::vector
<std::pair
<std::string
, UTILS::COLOR::ColorInfo
>>& colors
,
112 CLog::Log(LOGDEBUG
, "Loading colors from file {}", filePath
);
114 if (!xmlDoc
.LoadFile(filePath
))
116 CLog::Log(LOGERROR
, "{} - Failed to load colors from file {}", __FUNCTION__
, filePath
);
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__
);
128 const TiXmlElement
* xmlColor
= pRootElement
->FirstChildElement("color");
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");
140 std::sort(colors
.begin(), colors
.end(), UTILS::COLOR::comparePairColorInfo
);