forked from ccf19881030/QtCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
colormaker.cpp
111 lines (89 loc) · 2.13 KB
/
colormaker.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "colormaker.h"
#include <QTimerEvent>
#include <QDateTime>
ColorMaker::ColorMaker(QObject *parent)
:QObject(parent)
,m_algorithm(RandomRGB)
,m_currentColor(Qt::black)
,m_nColorTimer(0)
{
qsrand(QDateTime::currentDateTime().toTime_t());
}
ColorMaker::~ColorMaker()
{
}
QColor ColorMaker::color() const
{
return m_currentColor;
}
void ColorMaker::setColor(const QColor &color)
{
m_currentColor=color;
emit colorChanged(m_currentColor);
}
QColor ColorMaker::timeColor() const
{
QTime time=QTime::currentTime();
int r=time.hour();
int g=time.minute()*2;
int b=time.second()*4;
return QColor::fromRgb(r,g,b);
}
ColorMaker::GenerateAlgorithm ColorMaker::algorithm() const
{
return m_algorithm;
}
void ColorMaker::setAlgorithm(GenerateAlgorithm algorithm)
{
m_algorithm=algorithm;
}
void ColorMaker::start()
{
if(m_nColorTimer==0)
{
m_nColorTimer=startTimer(1000);
}
}
void ColorMaker::stop()
{
if(m_nColorTimer>0)
{
killTimer(m_nColorTimer);
m_nColorTimer=0;
}
}
void ColorMaker::timerEvent(QTimerEvent *e)
{
if(e->timerId()==m_nColorTimer)
{
switch(m_algorithm)
{
case RandomRGB:
m_currentColor.setRgb(qrand()%255,qrand()%255,qrand()%255);
break;
case RandomRed:
m_currentColor.setRed(qrand()%255);
break;
case RandomGreen:
m_currentColor.setGreen(qrand()%255);
break;
case RandomBlue:
m_currentColor.setBlue(qrand()%255);
break;
case LinearIncrease:
{
int r=m_currentColor.red()+10;
int g=m_currentColor.green()+10;
int b=m_currentColor.blue()+10;
m_currentColor.setRgb(r%255,g%255,b%255);
}
break;
}
emit colorChanged(m_currentColor);
emit currentTime(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
}
else
{
QObject::timerEvent(e);
}
}