summaryrefslogtreecommitdiffstats
path: root/kcoloredit/kzcolorselector.cpp
blob: c705cad0501b88ff12ed01f38941e7f02c449471 (plain)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/***************************************************************************
                          kzcolorselector.cpp  -  description
                             -------------------
    begin                : Fri Jul 14 2000
    copyright            : (C) 2000 by Artur Rataj
    email                : art@zeus.polsl.gliwice.pl
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include <tqpainter.h>
#include <tqimage.h>
#include <kimageeffect.h>
#include <kpalette.h>

#include "main.h"
#include "kzcolorselector.h"

KZColorSelector::KZColorSelector(Qt::Orientation o, TQWidget *parent, const char *name) :
	KSelector(o, parent, name) {
	baseColorH = -1;
	baseColorS = 0;
	baseColorV = 0;
	pixmap.setOptimization( TQPixmap::BestOptim );
}
KZColorSelector::~KZColorSelector() {
}

void KZColorSelector::setType(const int type) {
	this->type = type;
	switch(this->type) {
		case TYPE_H:
			setRange(0, HSV_MAX_H_VALUE);
			break;
			
		case TYPE_S:
			setRange(0, HSV_MAX_S_VALUE);
			break;
			
		case TYPE_V:
			setRange(0, HSV_MAX_V_VALUE);
			break;
			
	}
}

void KZColorSelector::updateContents() {
	drawPalette(&pixmap);
	tqrepaint(false);
}

void KZColorSelector::resizeEvent(TQResizeEvent*) {
	updateContents();
}

void KZColorSelector::drawContents(TQPainter* painter) {
	painter->drawPixmap(contentsRect().x(), contentsRect().y(), pixmap);
}

void KZColorSelector::setBaseColor(const TQColor& color) {
	color.hsv(&baseColorH, &baseColorS, &baseColorV);
}

void KZColorSelector::setBaseColorHsv(const int colorH,
	const int colorS, const int colorV) {
	baseColorH = colorH;
	baseColorS = colorS;
	baseColorV = colorV;
}

void KZColorSelector::updatePointerPos() {
	int pos;
	switch(type) {
		case TYPE_H:
			pos = baseColorH;
			if(pos < 0)
				pos = 0;
			break;
			
		case TYPE_S:
			pos = baseColorS;
			break;
			
		case TYPE_V:
			pos = baseColorV;
			break;
			
		default:
			pos = 0;
			break;
			
	}
	setValue(pos);
}

void KZColorSelector::setColor(TQColor* const color, const int y) {
	int ySize = contentsRect().height();
	switch(type) {
		case TYPE_H:
			color->setHsv(( ySize - 1 - y )*360/ySize, baseColorS, baseColorV);
			break;
			
		case TYPE_S:
			color->setHsv(baseColorH, ( ySize - 1 - y )*256/ySize, baseColorV);
			break;
			
		case TYPE_V:
			color->setHsv(baseColorH, baseColorS, ( ySize - 1 - y )*256/ySize);
			break;
			
	}
}

TQColor* KZColorSelector::getStandardColorsPalette() {
	TQColor* palette = new TQColor[( int )STANDARD_PALETTE_SIZE];
	int i = 0;
	palette[i++] = TQt::red;
	palette[i++] = TQt::green;
	palette[i++] = TQt::blue;
	palette[i++] = TQt::cyan;
	palette[i++] = TQt::magenta;
	palette[i++] = TQt::yellow;
	palette[i++] = TQt::darkRed;
	palette[i++] = TQt::darkGreen;
	palette[i++] = TQt::darkBlue;
	palette[i++] = TQt::darkCyan;
	palette[i++] = TQt::darkMagenta;
	palette[i++] = TQt::darkYellow;
	palette[i++] = TQt::white;
	palette[i++] = TQt::lightGray;
	palette[i++] = TQt::gray;
	palette[i++] = TQt::darkGray;
	palette[i++] = TQt::black;
	return palette;
}

void KZColorSelector::drawPalette(TQPixmap* pixmap) {
	int xSize = contentsRect().width();
	int ySize = contentsRect().height();
	TQImage image(xSize, ySize, 32);
	TQColor color;
	int x;
	int y;

	for (y = 0; y < ySize; ++y)
	{
		unsigned int* p = (unsigned int*)image.scanLine(y);
		for(x = 0; x < xSize; ++x)
		{
			setColor(&color, y);
			*p = color.rgb();
			++p;
		}
	}
	if (TQColor::numBitPlanes() <= 8)
	{
    TQColor* standardPalette = getStandardColorsPalette();
		KImageEffect::dither(image, standardPalette, STANDARD_PALETTE_SIZE);
		delete[] standardPalette;
	}
	pixmap->convertFromImage(image);
}
#include "kzcolorselector.moc"