sneedmc/application/ColorCache.h

120 lines
2.5 KiB
C
Raw Normal View History

2015-08-19 23:49:03 +00:00
#pragma once
#include <QtGui/QColor>
#include <rainbow.h>
#include <MessageLevel.h>
2015-08-19 23:49:03 +00:00
#include <QMap>
class ColorCache
{
public:
2018-07-15 12:51:05 +00:00
ColorCache(QColor front, QColor back, qreal bias)
{
m_front = front;
m_back = back;
m_bias = bias;
};
2015-08-19 23:49:03 +00:00
2018-07-15 12:51:05 +00:00
void addColor(int key, QColor color)
{
m_colors[key] = {color, blend(color), blendBackground(color)};
}
2015-08-19 23:49:03 +00:00
2018-07-15 12:51:05 +00:00
void setForeground(QColor front)
{
if(m_front != front)
{
m_front = front;
recolorAll();
}
}
2015-08-19 23:49:03 +00:00
2018-07-15 12:51:05 +00:00
void setBackground(QColor back)
{
if(m_back != back)
{
m_back = back;
recolorAll();
}
}
2015-08-19 23:49:03 +00:00
2018-07-15 12:51:05 +00:00
QColor getFront(int key)
{
auto iter = m_colors.find(key);
if(iter == m_colors.end())
{
return QColor();
}
return (*iter).front;
}
2015-08-19 23:49:03 +00:00
2018-07-15 12:51:05 +00:00
QColor getBack(int key)
{
auto iter = m_colors.find(key);
if(iter == m_colors.end())
{
return QColor();
}
return (*iter).back;
}
2015-08-19 23:49:03 +00:00
2018-07-15 12:51:05 +00:00
/**
* Blend the color with the front color, adapting to the back color
*/
QColor blend(QColor color);
2015-08-19 23:49:03 +00:00
2018-07-15 12:51:05 +00:00
/**
* Blend the color with the back color
*/
QColor blendBackground(QColor color);
2015-08-19 23:49:03 +00:00
protected:
2018-07-15 12:51:05 +00:00
void recolorAll();
2015-08-19 23:49:03 +00:00
protected:
2018-07-15 12:51:05 +00:00
struct ColorEntry
{
QColor original;
QColor front;
QColor back;
};
2015-08-19 23:49:03 +00:00
protected:
2018-07-15 12:51:05 +00:00
qreal m_bias;
QColor m_front;
QColor m_back;
QMap<int, ColorEntry> m_colors;
2015-08-19 23:49:03 +00:00
};
class LogColorCache : public ColorCache
{
public:
2018-07-15 12:51:05 +00:00
LogColorCache(QColor front, QColor back)
: ColorCache(front, back, 1.0)
{
addColor((int)MessageLevel::MultiMC, QColor("purple"));
addColor((int)MessageLevel::Debug, QColor("green"));
addColor((int)MessageLevel::Warning, QColor("orange"));
addColor((int)MessageLevel::Error, QColor("red"));
addColor((int)MessageLevel::Fatal, QColor("red"));
addColor((int)MessageLevel::Message, front);
}
2015-08-19 23:49:03 +00:00
2018-07-15 12:51:05 +00:00
QColor getFront(MessageLevel::Enum level)
{
if(!m_colors.contains((int) level))
{
return ColorCache::getFront((int)MessageLevel::Message);
}
return ColorCache::getFront((int)level);
}
2015-08-19 23:49:03 +00:00
2018-07-15 12:51:05 +00:00
QColor getBack(MessageLevel::Enum level)
{
if(level == MessageLevel::Fatal)
{
return QColor(Qt::black);
}
return QColor(Qt::transparent);
}
2015-08-19 23:49:03 +00:00
};