TDE graphics utilities
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

171 lines
4.2KB

  1. /***************************************************************************
  2. kzcolorselector.cpp - description
  3. -------------------
  4. begin : Fri Jul 14 2000
  5. copyright : (C) 2000 by Artur Rataj
  6. email : art@zeus.polsl.gliwice.pl
  7. ***************************************************************************/
  8. /***************************************************************************
  9. * *
  10. * This program is free software; you can redistribute it and/or modify *
  11. * it under the terms of the GNU General Public License as published by *
  12. * the Free Software Foundation; either version 2 of the License, or *
  13. * (at your option) any later version. *
  14. * *
  15. ***************************************************************************/
  16. #include <tqpainter.h>
  17. #include <tqimage.h>
  18. #include <kimageeffect.h>
  19. #include <kpalette.h>
  20. #include "main.h"
  21. #include "kzcolorselector.h"
  22. KZColorSelector::KZColorSelector(Qt::Orientation o, TQWidget *parent, const char *name) :
  23. KSelector(o, parent, name) {
  24. baseColorH = -1;
  25. baseColorS = 0;
  26. baseColorV = 0;
  27. pixmap.setOptimization( TQPixmap::BestOptim );
  28. }
  29. KZColorSelector::~KZColorSelector() {
  30. }
  31. void KZColorSelector::setType(const int type) {
  32. this->type = type;
  33. switch(this->type) {
  34. case TYPE_H:
  35. setRange(0, HSV_MAX_H_VALUE);
  36. break;
  37. case TYPE_S:
  38. setRange(0, HSV_MAX_S_VALUE);
  39. break;
  40. case TYPE_V:
  41. setRange(0, HSV_MAX_V_VALUE);
  42. break;
  43. }
  44. }
  45. void KZColorSelector::updateContents() {
  46. drawPalette(&pixmap);
  47. repaint(false);
  48. }
  49. void KZColorSelector::resizeEvent(TQResizeEvent*) {
  50. updateContents();
  51. }
  52. void KZColorSelector::drawContents(TQPainter* painter) {
  53. painter->drawPixmap(contentsRect().x(), contentsRect().y(), pixmap);
  54. }
  55. void KZColorSelector::setBaseColor(const TQColor& color) {
  56. color.hsv(&baseColorH, &baseColorS, &baseColorV);
  57. }
  58. void KZColorSelector::setBaseColorHsv(const int colorH,
  59. const int colorS, const int colorV) {
  60. baseColorH = colorH;
  61. baseColorS = colorS;
  62. baseColorV = colorV;
  63. }
  64. void KZColorSelector::updatePointerPos() {
  65. int pos;
  66. switch(type) {
  67. case TYPE_H:
  68. pos = baseColorH;
  69. if(pos < 0)
  70. pos = 0;
  71. break;
  72. case TYPE_S:
  73. pos = baseColorS;
  74. break;
  75. case TYPE_V:
  76. pos = baseColorV;
  77. break;
  78. default:
  79. pos = 0;
  80. break;
  81. }
  82. setValue(pos);
  83. }
  84. void KZColorSelector::setColor(TQColor* const color, const int y) {
  85. int ySize = contentsRect().height();
  86. switch(type) {
  87. case TYPE_H:
  88. color->setHsv(( ySize - 1 - y )*360/ySize, baseColorS, baseColorV);
  89. break;
  90. case TYPE_S:
  91. color->setHsv(baseColorH, ( ySize - 1 - y )*256/ySize, baseColorV);
  92. break;
  93. case TYPE_V:
  94. color->setHsv(baseColorH, baseColorS, ( ySize - 1 - y )*256/ySize);
  95. break;
  96. }
  97. }
  98. TQColor* KZColorSelector::getStandardColorsPalette() {
  99. TQColor* palette = new TQColor[( int )STANDARD_PALETTE_SIZE];
  100. int i = 0;
  101. palette[i++] = TQt::red;
  102. palette[i++] = TQt::green;
  103. palette[i++] = TQt::blue;
  104. palette[i++] = TQt::cyan;
  105. palette[i++] = TQt::magenta;
  106. palette[i++] = TQt::yellow;
  107. palette[i++] = TQt::darkRed;
  108. palette[i++] = TQt::darkGreen;
  109. palette[i++] = TQt::darkBlue;
  110. palette[i++] = TQt::darkCyan;
  111. palette[i++] = TQt::darkMagenta;
  112. palette[i++] = TQt::darkYellow;
  113. palette[i++] = TQt::white;
  114. palette[i++] = TQt::lightGray;
  115. palette[i++] = TQt::gray;
  116. palette[i++] = TQt::darkGray;
  117. palette[i++] = TQt::black;
  118. return palette;
  119. }
  120. void KZColorSelector::drawPalette(TQPixmap* pixmap) {
  121. int xSize = contentsRect().width();
  122. int ySize = contentsRect().height();
  123. TQImage image(xSize, ySize, 32);
  124. TQColor color;
  125. int x;
  126. int y;
  127. for (y = 0; y < ySize; ++y)
  128. {
  129. unsigned int* p = (unsigned int*)image.scanLine(y);
  130. for(x = 0; x < xSize; ++x)
  131. {
  132. setColor(&color, y);
  133. *p = color.rgb();
  134. ++p;
  135. }
  136. }
  137. if (TQColor::numBitPlanes() <= 8)
  138. {
  139. TQColor* standardPalette = getStandardColorsPalette();
  140. KImageEffect::dither(image, standardPalette, STANDARD_PALETTE_SIZE);
  141. delete[] standardPalette;
  142. }
  143. pixmap->convertFromImage(image);
  144. }
  145. #include "kzcolorselector.moc"