docs: describe the pmdaroot process interfaces
[pcp.git] / src / pmchart / colorscheme.cpp
blob7c14bc04040342267d2207d479aab2e3501f917d
1 /*
2 * Copyright (c) 2007, Aconex. All Rights Reserved.
3 * Copyright (c) 2013, Red Hat, Inc.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
16 #include "colorscheme.h"
17 #include "main.h"
19 ColorScheme::ColorScheme()
21 my.isModified = false;
22 my.name = QString::null;
25 bool ColorScheme::lookupScheme(QString name)
27 for (int i = 0; i < globalSettings.colorSchemes.size(); i++)
28 if (name == globalSettings.colorSchemes[i].name())
29 return true;
30 return false;
33 ColorScheme *ColorScheme::findScheme(QString name)
35 for (int i = 0; i < globalSettings.colorSchemes.size(); i++)
36 if (name == globalSettings.colorSchemes[i].name())
37 return &globalSettings.colorSchemes[i];
38 return NULL;
41 bool ColorScheme::removeScheme(QString name)
43 for (int i = 0; i < globalSettings.colorSchemes.size(); i++)
44 if (name == globalSettings.colorSchemes[i].name()) {
45 globalSettings.colorSchemes.removeAt(i);
46 return true;
48 return false;
51 static inline int hexval(float f)
53 return ((int)(0.5 + f*256) < 256 ? (int)(0.5 + f*256) : 256);
56 QColor ColorScheme::colorSpec(QString name)
58 QColor color;
59 QString rgbi = name;
61 if (rgbi.left(5) != "rgbi:")
62 color.setNamedColor(name);
63 else {
64 float fr, fg, fb;
65 if (sscanf((const char *)rgbi.toLatin1(), "rgbi:%f/%f/%f", &fr, &fg, &fb) == 3)
66 color.setRgb(hexval(fr), hexval(fg), hexval(fb));
67 // else return color as-is, i.e. invalid.
69 return color;
72 void ColorScheme::clear()
74 my.colors.clear();
75 my.colorNames.clear();
78 void ColorScheme::setColorNames(QStringList colorNames)
80 my.colorNames = colorNames;
81 for (int i = 0; i < colorNames.size(); i++)
82 my.colors << QColor(colorNames.at(i));
85 void ColorScheme::addColor(QColor color)
87 my.colors.append(color);
88 my.colorNames.append(color.name());
91 void ColorScheme::addColor(QString name)
93 my.colors.append(colorSpec(name));
94 my.colorNames.append(name);