gcsx_color.cpp 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  1. /* GCSx
  2. ** COLOR.CPP
  3. **
  4. ** Color conversion and selection widget
  5. */
  6. /*****************************************************************************
  7. ** Copyright (C) 2003-2006 Janson
  8. **
  9. ** This program is free software; you can redistribute it and/or modify
  10. ** it under the terms of the GNU General Public License as published by
  11. ** the Free Software Foundation; either version 2 of the License, or
  12. ** (at your option) any later version.
  13. **
  14. ** This program is distributed in the hope that it will be useful,
  15. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ** GNU General Public License for more details.
  18. **
  19. ** You should have received a copy of the GNU General Public License
  20. ** along with this program; if not, write to the Free Software
  21. ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
  22. *****************************************************************************/
  23. #include "all.h"
  24. // Color RGB<->HSL
  25. void color::RGBtoHSL() { start_func
  26. float r = R / (float)max;
  27. float g = G / (float)max;
  28. float b = B / (float)max;
  29. float minRGB = min(r, min(g, b));
  30. float maxRGB = std::max(r, std::max(g, b));
  31. L = (minRGB + maxRGB) / 2.0;
  32. if (minRGB == maxRGB) {
  33. S = H = 0.0;
  34. }
  35. else {
  36. if (L < 0.5) S = (maxRGB - minRGB) / (maxRGB + minRGB);
  37. else S = (maxRGB - minRGB) / (2.0 - maxRGB - minRGB);
  38. if (r == maxRGB) H = (g - b) / (maxRGB - minRGB);
  39. else if (g == maxRGB) H = 2.0 + (b - r) / (maxRGB - minRGB);
  40. else H = 4.0 + (r - g) / (maxRGB - minRGB);
  41. if (H < 0) H += 6.0;
  42. H /= 6.0;
  43. }
  44. Hi = (int)(H * 255.0 + 0.5);
  45. Si = (int)(S * 255.0 + 0.5);
  46. Li = (int)(L * 255.0 + 0.5);
  47. }
  48. void color::HSLtoRGB() { start_func
  49. float r, g, b;
  50. H = Hi / 255.0;
  51. S = Si / 255.0;
  52. L = Li / 255.0;
  53. if (S == 0.0) {
  54. r = g = b = L;
  55. }
  56. else {
  57. float t1, t2;
  58. float tR, tG, tB;
  59. if (L < 0.5) t2 = L * (1.0 + S);
  60. else t2 = L + S - L * S;
  61. t1 = 2.0 * L - t2;
  62. tR = H + 1.0 / 3.0;
  63. tG = H;
  64. tB = H - 1.0 / 3.0;
  65. if (tR > 1.0) tR -= 1.0;
  66. if (tB < 0) tB += 1.0;
  67. if (6.0 * tR < 1) r = t1 + (t2 - t1) * 6.0 * tR;
  68. else if (2.0 * tR < 1) r = t2;
  69. else if (3.0 * tR < 2) r = t1 + (t2 - t1) * (2.0 / 3.0 - tR) * 6.0;
  70. else r = t1;
  71. if (6.0 * tG < 1) g = t1 + (t2 - t1) * 6.0 * tG;
  72. else if (2.0 * tG < 1) g = t2;
  73. else if (3.0 * tG < 2) g = t1 + (t2 - t1) * (2.0 / 3.0 - tG) * 6.0;
  74. else g = t1;
  75. if (6.0 * tB < 1) b = t1 + (t2 - t1) * 6.0 * tB;
  76. else if (2.0 * tB < 1) b = t2;
  77. else if (3.0 * tB < 2) b = t1 + (t2 - t1) * (2.0 / 3.0 - tB) * 6.0;
  78. else b = t1;
  79. }
  80. R = (int)(r * max + 0.5);
  81. G = (int)(g * max + 0.5);
  82. B = (int)(b * max + 0.5);
  83. }
  84. // RGBHSL selection widget
  85. WRGBSelect::WRGBSelect(int wId, int w, int h, color* wSetting, int rgbBitDepth) : Widget(wId, blankString, wSetting) { start_func
  86. assert(wSetting);
  87. assert(w > 0);
  88. assert(h > 0);
  89. // Load immediately so we have a max
  90. currentColor = *((color*)wSetting);
  91. maxComponent = (1 << rgbBitDepth) - 1;
  92. innerWidth = w;
  93. innerHeight = h;
  94. zPanelX = innerWidth + WRGBSELECT_BORDER * 2 + WRGBSELECT_GUTTER;
  95. aX = AXIS_H;
  96. aY = AXIS_S;
  97. aZ = AXIS_L;
  98. fX = 0;
  99. fY = 1;
  100. fZ = 1;
  101. dependXYtoZ = 1;
  102. dragMode = WRGBSELECT_DRAG_NONE;
  103. // Attempt to allocate video surfaces
  104. xyPanel = NULL;
  105. zPanel = NULL;
  106. xyPanel = createSurface(w, h);
  107. zPanel = createSurface(WRGBSELECT_ZWIDTH, h);
  108. resize(w + WRGBSELECT_BORDER * 4 + WRGBSELECT_GUTTER + WRGBSELECT_ZWIDTH,
  109. h + WRGBSELECT_BORDER * 2);
  110. }
  111. WRGBSelect::~WRGBSelect() { start_func
  112. if (xyPanel) SDL_FreeSurface(xyPanel);
  113. if (zPanel) SDL_FreeSurface(zPanel);
  114. }
  115. void WRGBSelect::setMode(int axisX, int axisY, int axisZ, int flipX, int flipY, int flipZ, int setDependXYtoZ) { start_func
  116. // Must be a valid combination
  117. assert(((axisX == AXIS_R) && (axisY == AXIS_G) && (axisZ == AXIS_B)) ||
  118. ((axisX == AXIS_G) && (axisY == AXIS_R) && (axisZ == AXIS_B)) ||
  119. ((axisX == AXIS_R) && (axisY == AXIS_B) && (axisZ == AXIS_G)) ||
  120. ((axisX == AXIS_B) && (axisY == AXIS_G) && (axisZ == AXIS_R)) ||
  121. ((axisX == AXIS_G) && (axisY == AXIS_B) && (axisZ == AXIS_R)) ||
  122. ((axisX == AXIS_B) && (axisY == AXIS_R) && (axisZ == AXIS_G)) ||
  123. ((axisX == AXIS_H) && (axisY == AXIS_S) && (axisZ == AXIS_L)) ||
  124. ((axisX == AXIS_S) && (axisY == AXIS_H) && (axisZ == AXIS_L)) ||
  125. ((axisX == AXIS_H) && (axisY == AXIS_L) && (axisZ == AXIS_S)) ||
  126. ((axisX == AXIS_L) && (axisY == AXIS_S) && (axisZ == AXIS_H)) ||
  127. ((axisX == AXIS_L) && (axisY == AXIS_H) && (axisZ == AXIS_S)) ||
  128. ((axisX == AXIS_S) && (axisY == AXIS_L) && (axisZ == AXIS_H)));
  129. aX = axisX;
  130. aY = axisY;
  131. aZ = axisZ;
  132. fX = flipX;
  133. fY = flipY;
  134. fZ = flipZ;
  135. dependXYtoZ = setDependXYtoZ;
  136. locateColor(1);
  137. setDirty(1);
  138. }
  139. int* WRGBSelect::convertAxis(color* src, int axis) { start_func
  140. assert((axis == AXIS_R) || (axis == AXIS_G) || (axis == AXIS_B) ||
  141. (axis == AXIS_H) || (axis == AXIS_S) || (axis == AXIS_L));
  142. // Returns a pointer to the item that corresponds to the given axis
  143. if (axis == AXIS_R) return &(src->R);
  144. if (axis == AXIS_G) return &(src->G);
  145. if (axis == AXIS_B) return &(src->B);
  146. if (axis == AXIS_H) return &(src->Hi);
  147. if (axis == AXIS_S) return &(src->Si);
  148. return &(src->Li);
  149. }
  150. int WRGBSelect::convertMax(const color* src, int axis) { start_func
  151. assert((axis == AXIS_R) || (axis == AXIS_G) || (axis == AXIS_B) ||
  152. (axis == AXIS_H) || (axis == AXIS_S) || (axis == AXIS_L));
  153. // Returns a pointer to the item that corresponds to the given axis
  154. if (axis == AXIS_R) return src->max;
  155. if (axis == AXIS_G) return src->max;
  156. if (axis == AXIS_B) return src->max;
  157. return 255;
  158. }
  159. void WRGBSelect::redrawXYPanel() { start_func
  160. color workArea = currentColor;
  161. int* xAxis;
  162. int* yAxis;
  163. int xMax;
  164. int yMax;
  165. int HSLmode = 0;
  166. SDL_PixelFormat* format = xyPanel->format;
  167. if (dependXYtoZ) {
  168. // RGB mode?
  169. if ((aX == AXIS_R) || (aX == AXIS_G) || (aX == AXIS_B)) {
  170. workArea.R = workArea.G = workArea.B = 0;
  171. }
  172. // HSL mode
  173. else {
  174. workArea.Hi = 0;
  175. workArea.Si = 255;
  176. workArea.Li = 128;
  177. HSLmode = 1;
  178. }
  179. }
  180. else if ((aX == AXIS_H) || (aX == AXIS_S) || (aX == AXIS_L)) HSLmode = 1;
  181. xAxis = convertAxis(&workArea, aX);
  182. yAxis = convertAxis(&workArea, aY);
  183. xMax = convertMax(&workArea, aX);
  184. yMax = convertMax(&workArea, aY);
  185. for (int pY = 0; pY < innerHeight; ++pY) {
  186. for (int pX = 0; pX < innerWidth; ++pX) {
  187. // Always flip using w/h, not 255
  188. if (fX) *xAxis = (((innerWidth - 1) - pX) * xMax + ((innerWidth - 1) / 2)) / (innerWidth - 1);
  189. else *xAxis = (pX * xMax + ((innerWidth - 1) / 2)) / (innerWidth - 1);
  190. if (fY) *yAxis = (((innerHeight - 1) - pY) * yMax + ((innerHeight - 1) / 2)) / (innerHeight - 1);
  191. else *yAxis = (pY * yMax + ((innerHeight - 1) / 2)) / (innerHeight - 1);
  192. if (HSLmode) workArea.HSLtoRGB();
  193. assert(workArea.max);
  194. drawPixel(pX, pY, SDL_MapRGB(format,
  195. scaleComponent(workArea.R, workArea.max, 255),
  196. scaleComponent(workArea.G, workArea.max, 255),
  197. scaleComponent(workArea.B, workArea.max, 255)),
  198. xyPanel);
  199. }
  200. }
  201. dirtyXY = 1;
  202. setDirty();
  203. }
  204. void WRGBSelect::redrawZPanel() { start_func
  205. color workArea = currentColor;
  206. int* zAxis;
  207. int zMax;
  208. int HSLmode = 0;
  209. SDL_PixelFormat* format = zPanel->format;
  210. if (!dependXYtoZ) {
  211. // RGB mode?
  212. if ((aZ == AXIS_R) || (aZ == AXIS_G) || (aZ == AXIS_B)) {
  213. workArea.R = workArea.G = workArea.B = 0;
  214. }
  215. // HSL mode
  216. else {
  217. workArea.Hi = 0;
  218. workArea.Si = 255;
  219. workArea.Li = 128;
  220. HSLmode = 1;
  221. }
  222. }
  223. else if ((aZ == AXIS_H) || (aZ == AXIS_S) || (aZ == AXIS_L)) HSLmode = 1;
  224. zAxis = convertAxis(&workArea, aZ);
  225. zMax = convertMax(&workArea, aZ);
  226. for (int pZ = 0; pZ < innerHeight; ++pZ) {
  227. // Always flip using w/h, not 255
  228. if (fZ) *zAxis = (((innerHeight - 1) - pZ) * zMax + ((innerHeight - 1) / 2)) / (innerHeight - 1);
  229. else *zAxis = (pZ * zMax + ((innerHeight - 1) / 2)) / (innerHeight - 1);
  230. if (HSLmode) workArea.HSLtoRGB();
  231. assert(workArea.max);
  232. drawHLine(0, WRGBSELECT_ZWIDTH - 1, pZ, SDL_MapRGB(format,
  233. scaleComponent(workArea.R, workArea.max, 255),
  234. scaleComponent(workArea.G, workArea.max, 255),
  235. scaleComponent(workArea.B, workArea.max, 255)),
  236. zPanel);
  237. }
  238. dirtyZ = 1;
  239. setDirty();
  240. }
  241. void WRGBSelect::setXY(int newX, int newY) { start_func
  242. // Clip
  243. if (newX < 0) newX = 0;
  244. if (newY < 0) newY = 0;
  245. if (newX >= innerWidth) newX = innerWidth - 1;
  246. if (newY >= innerHeight) newY = innerHeight - 1;
  247. if ((newX != xSelected) || (newY != ySelected)) {
  248. xSelected = newX;
  249. ySelected = newY;
  250. // Always flip using w/h, not 255
  251. if (fX) newX = (innerWidth - 1) - newX;
  252. if (fY) newY = (innerHeight - 1) - newY;
  253. int xymax = convertMax(&currentColor, aX);
  254. int rgbX = (newX * xymax + ((innerWidth - 1) / 2)) / (innerWidth - 1);
  255. int rgbY = (newY * xymax + ((innerHeight - 1) / 2)) / (innerHeight - 1);
  256. *(convertAxis(&currentColor, aX)) = rgbX;
  257. *(convertAxis(&currentColor, aY)) = rgbY;
  258. // HSL mode or RGB mode? Keep both halves current
  259. if ((aX == AXIS_H) || (aX == AXIS_S) || (aX == AXIS_L)) currentColor.HSLtoRGB();
  260. else currentColor.RGBtoHSL();
  261. if (dependXYtoZ) redrawZPanel();
  262. dirtyXY = 1;
  263. setDirty();
  264. // @TODO: 3 exceptions
  265. parent->childModified(this);
  266. }
  267. }
  268. void WRGBSelect::setZ(int newZ) { start_func
  269. // Clip
  270. if (newZ < 0) newZ = 0;
  271. if (newZ >= innerHeight) newZ = innerHeight - 1;
  272. if (newZ != zSelected) {
  273. zSelected = newZ;
  274. // Always flip using w/h, not 255
  275. if (fZ) newZ = (innerHeight - 1) - newZ;
  276. int zmax = convertMax(&currentColor, aZ);
  277. int rgbZ = (newZ * zmax + ((innerHeight - 1) / 2)) / (innerHeight - 1);
  278. *(convertAxis(&currentColor, aZ)) = rgbZ;
  279. // HSL mode or RGB mode? Keep both halves current
  280. if ((aZ == AXIS_H) || (aZ == AXIS_S) || (aZ == AXIS_L)) currentColor.HSLtoRGB();
  281. else currentColor.RGBtoHSL();
  282. if (!dependXYtoZ) redrawXYPanel();
  283. dirtyZ = 1;
  284. setDirty();
  285. // @TODO: 3 exceptions
  286. parent->childModified(this);
  287. }
  288. }
  289. void WRGBSelect::locateColor(int fullredraw) { start_func
  290. int newX = *(convertAxis(&currentColor, aX)) * (innerWidth - 1) / convertMax(&currentColor, aX);
  291. int newY = *(convertAxis(&currentColor, aY)) * (innerHeight - 1) / convertMax(&currentColor, aY);
  292. int newZ = *(convertAxis(&currentColor, aZ)) * (innerHeight - 1) / convertMax(&currentColor, aZ);
  293. // Always flip using w/h, not 255
  294. if (fX) newX = (innerWidth - 1) - newX;
  295. if (fY) newY = (innerHeight - 1) - newY;
  296. if (fZ) newZ = (innerHeight - 1) - newZ;
  297. if ((xSelected != newX) || (ySelected != newY)) {
  298. xSelected = newX;
  299. ySelected = newY;
  300. if ((dependXYtoZ) || (fullredraw)) redrawZPanel();
  301. dirtyXY = 1;
  302. setDirty();
  303. }
  304. else if (fullredraw) redrawZPanel();
  305. if (zSelected != newZ) {
  306. zSelected = newZ;
  307. if ((!dependXYtoZ) || (fullredraw)) redrawXYPanel();
  308. dirtyZ = 1;
  309. setDirty();
  310. }
  311. else if (fullredraw) redrawXYPanel();
  312. }
  313. int WRGBSelect::event(int hasFocus, const SDL_Event* event) { start_func
  314. assert(event);
  315. assert(parent);
  316. int amount;
  317. switch (event->type) {
  318. case SDL_INPUTFOCUS:
  319. if (event->user.code & 1) {
  320. if (!haveFocus) {
  321. haveFocus = 1;
  322. setDirty(1);
  323. }
  324. }
  325. else {
  326. if (haveFocus) {
  327. haveFocus = 0;
  328. setDirty(1);
  329. }
  330. }
  331. return 1;
  332. case SDL_MOUSEBUTTONDOWN:
  333. case SDL_MOUSEBUTTONDBL:
  334. if (event->button.button == SDL_BUTTON_LEFT) {
  335. dragMode = WRGBSELECT_DRAG_NONE;
  336. if ((event->button.y >= WRGBSELECT_BORDER) && (event->button.y < height - WRGBSELECT_BORDER)) {
  337. if ((event->button.x >= WRGBSELECT_BORDER) && (event->button.x < innerWidth + WRGBSELECT_BORDER)) {
  338. setXY(event->button.x - WRGBSELECT_BORDER, event->button.y - WRGBSELECT_BORDER);
  339. dragMode = WRGBSELECT_DRAG_XY;
  340. }
  341. if ((event->button.x >= zPanelX) && (event->button.x < zPanelX + WRGBSELECT_ZWIDTH)) {
  342. setZ(event->button.y - WRGBSELECT_BORDER);
  343. dragMode = WRGBSELECT_DRAG_Z;
  344. }
  345. }
  346. return 1;
  347. }
  348. break;
  349. case SDL_MOUSEBUTTONUP:
  350. if (event->button.button == SDL_BUTTON_LEFT) {
  351. dragMode = WRGBSELECT_DRAG_NONE;
  352. return 1;
  353. }
  354. break;
  355. case SDL_MOUSEMOTION:
  356. if (event->motion.state & SDL_BUTTON_LMASK) {
  357. if (dragMode == WRGBSELECT_DRAG_XY) {
  358. setXY((Sint16)event->motion.x - WRGBSELECT_BORDER, (Sint16)event->motion.y - WRGBSELECT_BORDER);
  359. }
  360. else if (dragMode == WRGBSELECT_DRAG_Z) {
  361. setZ((Sint16)event->button.y - WRGBSELECT_BORDER);
  362. }
  363. return 1;
  364. }
  365. break;
  366. case SDL_KEYDOWN:
  367. amount = (event->key.keysym.mod & KMOD_CTRL) ? 10 : 1;
  368. switch (combineKey(event->key.keysym.sym, event->key.keysym.mod & (~KMOD_CTRL))) {
  369. case SDLK_RIGHT:
  370. setXY(xSelected + amount, ySelected);
  371. return 1;
  372. case SDLK_DOWN:
  373. setXY(xSelected, ySelected + amount);
  374. return 1;
  375. case SDLK_LEFT:
  376. setXY(xSelected - amount, ySelected);
  377. return 1;
  378. case SDLK_UP:
  379. setXY(xSelected, ySelected - amount);
  380. return 1;
  381. case SDLK_PAGEUP:
  382. setZ(zSelected - amount);
  383. return 1;
  384. case SDLK_PAGEDOWN:
  385. setZ(zSelected + amount);
  386. return 1;
  387. }
  388. break;
  389. }
  390. return 0;
  391. }
  392. void WRGBSelect::load() { start_func
  393. currentColor = *((color*)setting);
  394. locateColor(0);
  395. }
  396. void WRGBSelect::apply() { start_func
  397. *((color*)setting) = currentColor;
  398. }
  399. void WRGBSelect::displayCursor(SDL_Surface* destSurface, int xCenter, int yCenter) { start_func
  400. drawRect(xCenter - (WRGBSELECT_CURSOR_THICK - 1) / 2 - WRGBSELECT_CURSOR_LENGTH,
  401. yCenter - (WRGBSELECT_CURSOR_THICK - 1) / 2,
  402. WRGBSELECT_CURSOR_LENGTH, WRGBSELECT_CURSOR_THICK,
  403. guiPacked[COLOR_TEXT], destSurface);
  404. drawRect(xCenter + (WRGBSELECT_CURSOR_THICK - 1) / 2 + 1,
  405. yCenter - (WRGBSELECT_CURSOR_THICK - 1) / 2,
  406. WRGBSELECT_CURSOR_LENGTH, WRGBSELECT_CURSOR_THICK,
  407. guiPacked[COLOR_TEXT], destSurface);
  408. drawRect(xCenter - (WRGBSELECT_CURSOR_THICK - 1) / 2,
  409. yCenter - (WRGBSELECT_CURSOR_THICK - 1) / 2 - WRGBSELECT_CURSOR_LENGTH,
  410. WRGBSELECT_CURSOR_THICK, WRGBSELECT_CURSOR_LENGTH,
  411. guiPacked[COLOR_TEXT], destSurface);
  412. drawRect(xCenter - (WRGBSELECT_CURSOR_THICK - 1) / 2,
  413. yCenter + (WRGBSELECT_CURSOR_THICK - 1) / 2 + 1,
  414. WRGBSELECT_CURSOR_THICK, WRGBSELECT_CURSOR_LENGTH,
  415. guiPacked[COLOR_TEXT], destSurface);
  416. drawRect(xCenter - (WRGBSELECT_CURSOR_THICK - 1) / 2 - WRGBSELECT_CURSOR_LENGTH + 1,
  417. yCenter - (WRGBSELECT_CURSOR_THICK - 1) / 2 + 1,
  418. WRGBSELECT_CURSOR_LENGTH - 2, WRGBSELECT_CURSOR_THICK - 2,
  419. guiPacked[COLOR_LIGHT1], destSurface);
  420. drawRect(xCenter + (WRGBSELECT_CURSOR_THICK - 1) / 2 + 2,
  421. yCenter - (WRGBSELECT_CURSOR_THICK - 1) / 2 + 1,
  422. WRGBSELECT_CURSOR_LENGTH - 2, WRGBSELECT_CURSOR_THICK - 2,
  423. guiPacked[COLOR_LIGHT1], destSurface);
  424. drawRect(xCenter - (WRGBSELECT_CURSOR_THICK - 1) / 2 + 1,
  425. yCenter - (WRGBSELECT_CURSOR_THICK - 1) / 2 - WRGBSELECT_CURSOR_LENGTH + 1,
  426. WRGBSELECT_CURSOR_THICK - 2, WRGBSELECT_CURSOR_LENGTH - 2,
  427. guiPacked[COLOR_LIGHT1], destSurface);
  428. drawRect(xCenter - (WRGBSELECT_CURSOR_THICK - 1) / 2 + 1,
  429. yCenter + (WRGBSELECT_CURSOR_THICK - 1) / 2 + 2,
  430. WRGBSELECT_CURSOR_THICK - 2, WRGBSELECT_CURSOR_LENGTH - 2,
  431. guiPacked[COLOR_LIGHT1], destSurface);
  432. if (haveFocus) {
  433. drawFocusBox(xCenter - (WRGBSELECT_CURSOR_THICK - 1) / 2 - WRGBSELECT_CURSOR_LENGTH,
  434. yCenter - (WRGBSELECT_CURSOR_THICK - 1) / 2 - WRGBSELECT_CURSOR_LENGTH,
  435. WRGBSELECT_CURSOR_LENGTH * 2 + WRGBSELECT_CURSOR_THICK,
  436. WRGBSELECT_CURSOR_LENGTH * 2 + WRGBSELECT_CURSOR_THICK,
  437. destSurface);
  438. }
  439. }
  440. void WRGBSelect::display(SDL_Surface* destSurface, Rect& toDisplay, const Rect& clipArea, int xOffset, int yOffset) { start_func
  441. assert(destSurface);
  442. if (visible) {
  443. // This breaks redraw down into xy and z panels
  444. // Redraw all?
  445. if (totalDirty) {
  446. getRect(toDisplay);
  447. toDisplay.x += xOffset;
  448. toDisplay.y += yOffset;
  449. intersectRects(toDisplay, clipArea);
  450. }
  451. xOffset += x;
  452. yOffset += y;
  453. // Anything to draw so far?
  454. if (toDisplay.w) {
  455. SDL_SetClipRect(destSurface, &toDisplay);
  456. // Draw fill
  457. SDL_FillRect(destSurface, &toDisplay, guiPacked[COLOR_FILL]);
  458. // Borders
  459. drawGuiBoxInvert(xOffset, yOffset, innerWidth + WRGBSELECT_BORDER * 2, innerHeight + WRGBSELECT_BORDER * 2, WRGBSELECT_BORDER, destSurface);
  460. drawGuiBoxInvert(xOffset + zPanelX, yOffset, WRGBSELECT_ZWIDTH + WRGBSELECT_BORDER * 2, innerHeight + WRGBSELECT_BORDER * 2, WRGBSELECT_BORDER, destSurface);
  461. }
  462. // This will be the final displayed amount, we update it as we go but still work
  463. // off of the original dirtied area for efficiency
  464. Rect updatedArea = toDisplay;
  465. // Determine clip area for XY panel
  466. Rect xyClip = { xOffset + WRGBSELECT_BORDER, yOffset + WRGBSELECT_BORDER, innerWidth, innerHeight };
  467. intersectRects(xyClip, clipArea);
  468. // If we need to update XY panel, OR it intersects with the dirty area,
  469. // redraw it; note that if the second conditional is tested, xyClip will be reduced
  470. // to whatever portion intersects with the dirty area, which is what we want
  471. if ((dirtyXY) || (intersectRects(xyClip, toDisplay))) {
  472. // Add panel rect into updated area
  473. boundRects(updatedArea, xyClip);
  474. SDL_SetClipRect(destSurface, &xyClip);
  475. // Colorful area
  476. blit(0, 0, xyPanel, xOffset + WRGBSELECT_BORDER, yOffset + WRGBSELECT_BORDER, destSurface, innerWidth, innerHeight);
  477. // Selection cursor
  478. displayCursor(destSurface, xOffset + WRGBSELECT_BORDER + xSelected, yOffset + WRGBSELECT_BORDER + ySelected);
  479. }
  480. // Determine clip area for Z panel
  481. Rect zClip = { xOffset + zPanelX + WRGBSELECT_BORDER, yOffset+ WRGBSELECT_BORDER, WRGBSELECT_ZWIDTH, innerHeight };
  482. intersectRects(zClip, clipArea);
  483. // If we need to update Z panel, OR it intersects with the dirty area,
  484. // redraw it; note that if the second conditional is tested, zClip will be reduced
  485. // to whatever portion intersects with the dirty area, which is what we want
  486. if ((dirtyZ) || (intersectRects(zClip, toDisplay))) {
  487. // Add panel rect into updated area
  488. boundRects(updatedArea, zClip);
  489. SDL_SetClipRect(destSurface, &zClip);
  490. // Colorful area
  491. blit(0, 0, zPanel, xOffset + zPanelX + WRGBSELECT_BORDER, yOffset + WRGBSELECT_BORDER, destSurface, WRGBSELECT_ZWIDTH, innerHeight);
  492. // Selection cursor
  493. displayCursor(destSurface, xOffset + zPanelX + WRGBSELECT_BORDER + WRGBSELECT_ZWIDTH / 2, yOffset + WRGBSELECT_BORDER + zSelected);
  494. }
  495. toDisplay = updatedArea;
  496. }
  497. dirty = dirtyXY = dirtyZ = totalDirty = 0;
  498. }
  499. void WRGBSelect::resolutionChange(int fromW, int fromH, int fromBpp, int toW, int toH, int toBpp) { start_func
  500. Window::resolutionChange(fromW, fromH, fromBpp, toW, toH, toBpp);
  501. if (fromBpp != toBpp) {
  502. if (xyPanel) SDL_FreeSurface(xyPanel);
  503. xyPanel = NULL;
  504. if (zPanel) SDL_FreeSurface(zPanel);
  505. zPanel = NULL;
  506. xyPanel = createSurface(innerWidth, innerHeight);
  507. zPanel = createSurface(WRGBSELECT_ZWIDTH, innerHeight);
  508. redrawXYPanel();
  509. redrawZPanel();
  510. }
  511. }
  512. // Color display widget
  513. WShowColor::WShowColor(int wId, int w, int h, color* wSetting) : Widget(wId, blankString, wSetting) { start_func
  514. assert(wSetting);
  515. assert(w > 0);
  516. assert(h > 0);
  517. resize(w + SHOWCOLOR_BORDER * 2, h + SHOWCOLOR_BORDER * 2);
  518. }
  519. WShowColor::~WShowColor() { start_func
  520. }
  521. int WShowColor::event(int hasFocus, const SDL_Event* event) { start_func
  522. // (no events accepted)
  523. return 0;
  524. }
  525. int WShowColor::refuseAll() const { start_func
  526. // (no events accepted)
  527. return 1;
  528. }
  529. void WShowColor::load() { start_func
  530. currentColor = *((color*)setting);
  531. setDirty();
  532. }
  533. void WShowColor::apply() { start_func
  534. // (doesn't apply)
  535. }
  536. void WShowColor::display(SDL_Surface* destSurface, Rect& toDisplay, const Rect& clipArea, int xOffset, int yOffset) { start_func
  537. assert(destSurface);
  538. if (visible) {
  539. // If dirty, redraw all
  540. if (dirty) {
  541. getRect(toDisplay);
  542. toDisplay.x += xOffset;
  543. toDisplay.y += yOffset;
  544. dirty = 0;
  545. intersectRects(toDisplay, clipArea);
  546. }
  547. // Anything to draw?
  548. if (toDisplay.w) {
  549. SDL_SetClipRect(destSurface, &toDisplay);
  550. xOffset += x;
  551. yOffset += y;
  552. // 0 alpha shows different
  553. if ((currentColor.A == 0) && (currentColor.maxAlpha)) {
  554. SDL_FillRect(destSurface, &toDisplay, guiPacked[COLOR_TRANSPARENT1]);
  555. drawLine(xOffset, yOffset, xOffset + width - 1, yOffset + height - 1,
  556. guiPacked[COLOR_TRANSPARENT2], destSurface);
  557. drawLine(xOffset + width - 1, yOffset, xOffset, yOffset + height - 1,
  558. guiPacked[COLOR_TRANSPARENT2], destSurface);
  559. }
  560. else {
  561. assert(currentColor.max);
  562. SDL_FillRect(destSurface, &toDisplay, SDL_MapRGB(destSurface->format,
  563. scaleComponent(currentColor.R, currentColor.max, 255),
  564. scaleComponent(currentColor.G, currentColor.max, 255),
  565. scaleComponent(currentColor.B, currentColor.max, 255)));
  566. if ((currentColor.A < currentColor.maxAlpha) && (currentColor.maxAlpha)) {
  567. drawRect(xOffset, yOffset + height / 2, width, height / 2,
  568. SDL_MapRGB(destSurface->format,
  569. scaleComponent(currentColor.R * currentColor.A / currentColor.maxAlpha, currentColor.max, 255),
  570. scaleComponent(currentColor.G * currentColor.A / currentColor.maxAlpha, currentColor.max, 255),
  571. scaleComponent(currentColor.B * currentColor.A / currentColor.maxAlpha, currentColor.max, 255)),
  572. destSurface);
  573. }
  574. }
  575. // @TODO: Corners of inverted box aren't grey
  576. drawGuiBoxInvert(xOffset, yOffset, width, height, SHOWCOLOR_BORDER, destSurface);
  577. }
  578. }
  579. }
  580. // RGB selection dialog
  581. RGBSelect* RGBSelect::dialog = NULL;
  582. RGBSelect* RGBSelect::create() { start_func
  583. if (!dialog) {
  584. dialog = new RGBSelect();
  585. }
  586. return dialog;
  587. }
  588. void RGBSelect::destroy() { start_func
  589. if (dialog) {
  590. delete dialog;
  591. dialog = NULL;
  592. }
  593. }
  594. const string RGBSelect::resourceError("Error loading resource/coloricon.bmp");
  595. RGBSelect::RGBSelect() : Dialog("Select Color", 1) { start_func
  596. initializedH = 0;
  597. iconSurface = NULL;
  598. }
  599. void RGBSelect::applyRGBMethod() { start_func
  600. WRGBSelect* widget = dynamic_cast<WRGBSelect*>(findWidget(ID_RGBSELECT));
  601. switch (method) {
  602. case ID_METHOD_HS:
  603. widget->setMode(WRGBSelect::AXIS_H, WRGBSelect::AXIS_S, WRGBSelect::AXIS_L,
  604. 0, 1, 1);
  605. break;
  606. case ID_METHOD_LH:
  607. widget->setMode(WRGBSelect::AXIS_H, WRGBSelect::AXIS_L, WRGBSelect::AXIS_S,
  608. 0, 1, 1);
  609. break;
  610. case ID_METHOD_SL:
  611. widget->setMode(WRGBSelect::AXIS_S, WRGBSelect::AXIS_L, WRGBSelect::AXIS_H,
  612. 1, 1, 0, 0);
  613. break;
  614. case ID_METHOD_RG:
  615. widget->setMode(WRGBSelect::AXIS_R, WRGBSelect::AXIS_G, WRGBSelect::AXIS_B,
  616. 0, 1, 1);
  617. break;
  618. case ID_METHOD_GB:
  619. widget->setMode(WRGBSelect::AXIS_G, WRGBSelect::AXIS_B, WRGBSelect::AXIS_R,
  620. 0, 1, 1);
  621. break;
  622. case ID_METHOD_BR:
  623. widget->setMode(WRGBSelect::AXIS_B, WRGBSelect::AXIS_R, WRGBSelect::AXIS_G,
  624. 0, 1, 1);
  625. break;
  626. }
  627. }
  628. int RGBSelect::run(unsigned char& r, unsigned char& g, unsigned char& b, unsigned char& a, int bitDepthAlpha, int bitDepth) { start_func
  629. method = config->readNum(RGBSELECT_METHOD);
  630. if ((method < ID_METHOD_HS) || (method > ID_METHOD_BR)) method = ID_METHOD_HS;
  631. rgbBitDepth = bitDepth;
  632. alphaBitDepth = bitDepthAlpha;
  633. int maxComponent = (1 << bitDepth) - 1;
  634. int maxAlpha = (1 << bitDepthAlpha) - 1;
  635. theColor.R = r;
  636. theColor.G = g;
  637. theColor.B = b;
  638. theColor.A = a;
  639. theColor.max = maxComponent;
  640. theColor.maxAlpha = maxAlpha;
  641. theColor.RGBtoHSL();
  642. if (initializedH) {
  643. initializedH = 0;
  644. clearWidgets();
  645. }
  646. else {
  647. string iconFile;
  648. createFilename(resourceDir->c_str(), FILENAME_COLORICONS, iconFile);
  649. iconSurface = SDL_LoadBMP(iconFile.c_str());
  650. if (iconSurface == NULL) {
  651. guiErrorBox(resourceError, errorTitleResourceLoad);
  652. }
  653. }
  654. Widget* w = NULL;
  655. WNumberBox* n = NULL;
  656. // We hide the top row and resize the color selection area as needed
  657. // to fit within current resolution
  658. if (screenHeight >= 350) {
  659. w = new WShowColor(ID_DEMOCOLOR, 30, 30, &theColor);
  660. w->setToolTip("Current color");
  661. w->addTo(this);
  662. w = new WRadioButton(ID_METHOD, iconSurface, 0, 0, 20, 20, &method, ID_METHOD_HS);
  663. w->setToolTip("Select by hue/saturation");
  664. w->addTo(this);
  665. w = new WRadioButton(ID_METHOD, iconSurface, 20, 0, 20, 20, &method, ID_METHOD_LH);
  666. w->setToolTip("Select by hue/luminance");
  667. w->addTo(this);
  668. w = new WRadioButton(ID_METHOD, iconSurface, 40, 0, 20, 20, &method, ID_METHOD_SL);
  669. w->setToolTip("Select by saturation/luminance");
  670. w->addTo(this);
  671. w = new WRadioButton(ID_METHOD, iconSurface, 60, 0, 20, 20, &method, ID_METHOD_RG);
  672. w->setToolTip("Select by red/green");
  673. w->addTo(this);
  674. w = new WRadioButton(ID_METHOD, iconSurface, 80, 0, 20, 20, &method, ID_METHOD_GB);
  675. w->setToolTip("Select by green/blue");
  676. w->addTo(this);
  677. w = new WRadioButton(ID_METHOD, iconSurface, 100, 0, 20, 20, &method, ID_METHOD_BR);
  678. w->setToolTip("Select by red/blue");
  679. w->addTo(this);
  680. arrangeRow();
  681. w = new WRGBSelect(ID_RGBSELECT, 208, min(208, screenHeight - 220), &theColor, rgbBitDepth);
  682. w->addTo(this);
  683. arrangeRow();
  684. }
  685. else {
  686. w = new WRGBSelect(ID_RGBSELECT, 208, screenHeight - 180, &theColor, rgbBitDepth);
  687. w->addTo(this);
  688. arrangeRow();
  689. }
  690. applyRGBMethod();
  691. w = new WStatic(ID_R_LABEL, "\tR:");
  692. w->addTo(this);
  693. w = n = new WNumberBox(ID_R_N, &theColor.R, 0, maxComponent);
  694. w->setToolTip("Red");
  695. w->addTo(this);
  696. w = new WSlider(ID_R_S, 0, maxComponent, &theColor.R);
  697. w->setToolTip("Red");
  698. dynamic_cast<WSlider*>(w)->linkTo(n);
  699. w->addTo(this);
  700. w = new WStatic(ID_H_LABEL, "\tH:");
  701. w->addTo(this);
  702. w = n = new WNumberBox(ID_H_N, &theColor.Hi, 0, 255);
  703. w->setToolTip("Hue (what color)");
  704. w->addTo(this);
  705. w = new WSlider(ID_H_S, 0, 255, &theColor.Hi);
  706. w->setToolTip("Hue (what color)");
  707. dynamic_cast<WSlider*>(w)->linkTo(n);
  708. w->addTo(this);
  709. w = new WStatic(ID_G_LABEL, "\tG:");
  710. w->addTo(this);
  711. w = n = new WNumberBox(ID_G_N, &theColor.G, 0, maxComponent);
  712. w->setToolTip("Green");
  713. w->addTo(this);
  714. w = new WSlider(ID_G_S, 0, maxComponent, &theColor.G);
  715. w->setToolTip("Green");
  716. dynamic_cast<WSlider*>(w)->linkTo(n);
  717. w->addTo(this);
  718. w = new WStatic(ID_S_LABEL, "\tS:");
  719. w->addTo(this);
  720. w = n = new WNumberBox(ID_S_N, &theColor.Si, 0, 255);
  721. w->setToolTip("Saturation (how colorful or grey)");
  722. w->addTo(this);
  723. w = new WSlider(ID_S_S, 0, 255, &theColor.Si);
  724. w->setToolTip("Saturation (how colorful or grey)");
  725. dynamic_cast<WSlider*>(w)->linkTo(n);
  726. w->addTo(this);
  727. w = new WStatic(ID_B_LABEL, "\tB:");
  728. w->addTo(this);
  729. w = n = new WNumberBox(ID_B_N, &theColor.B, 0, maxComponent);
  730. w->setToolTip("Blue");
  731. w->addTo(this);
  732. w = new WSlider(ID_B_S, 0, maxComponent, &theColor.B);
  733. w->setToolTip("Blue");
  734. dynamic_cast<WSlider*>(w)->linkTo(n);
  735. w->addTo(this);
  736. w = new WStatic(ID_L_LABEL, "\tL:");
  737. w->addTo(this);
  738. w = n = new WNumberBox(ID_L_N, &theColor.Li, 0, 255);
  739. w->setToolTip("Luminance (how bright)");
  740. w->addTo(this);
  741. w = new WSlider(ID_L_S, 0, 255, &theColor.Li);
  742. w->setToolTip("Luminance (how bright)");
  743. dynamic_cast<WSlider*>(w)->linkTo(n);
  744. w->addTo(this);
  745. w = new WStatic(ID_A_LABEL, "\tA:");
  746. w->addTo(this);
  747. w = n = new WNumberBox(ID_A_N, &theColor.A, 0, maxAlpha);
  748. w->setToolTip("Alpha (blend percent)");
  749. w->addTo(this);
  750. w = new WSlider(ID_A_S, 0, maxAlpha, &theColor.A);
  751. w->setToolTip("Alpha (blend percent)");
  752. dynamic_cast<WSlider*>(w)->linkTo(n);
  753. w->addTo(this);
  754. w = new WButton(ID_OK, messageBoxOK, Dialog::BUTTON_OK);
  755. w->addTo(this);
  756. w = new WButton(ID_CANCEL, messageBoxCancel, Dialog::BUTTON_CANCEL);
  757. w->addTo(this);
  758. makePretty(6, 1, 1, 1);
  759. initializedH = screenHeight;
  760. if (bitDepthAlpha) {
  761. findWidget(ID_A_LABEL)->enable();
  762. findWidget(ID_A_N)->enable();
  763. findWidget(ID_A_S)->enable();
  764. }
  765. else {
  766. findWidget(ID_A_LABEL)->disable();
  767. findWidget(ID_A_N)->disable();
  768. findWidget(ID_A_S)->disable();
  769. }
  770. int result = runModal();
  771. if (result == ID_OK) {
  772. config->write(RGBSELECT_METHOD, method);
  773. r = theColor.R;
  774. g = theColor.G;
  775. b = theColor.B;
  776. a = theColor.A;
  777. return 1;
  778. }
  779. return 0;
  780. }
  781. void RGBSelect::childModified(Window* modified) { start_func
  782. Dialog::childModified(modified);
  783. Widget* widget = dynamic_cast<Widget*>(modified);
  784. if (widget) {
  785. if ((!inSiblingModify) && (initializedH)) {
  786. Dialog::childModified(modified);
  787. inSiblingModify = 1;
  788. int id = widget->getId();
  789. // If method changed, update RGB control
  790. if (id == ID_METHOD) {
  791. applyRGBMethod();
  792. }
  793. // If selector updated, other stuff reloads
  794. if (id == ID_RGBSELECT) {
  795. for (int pos = ID_R_N; pos <= ID_L_S; ++pos) {
  796. findWidget(pos)->load();
  797. }
  798. }
  799. // If RGB updated, copy to HSL
  800. if ((id >= ID_R_N) && (id <= ID_B_S)) {
  801. theColor.RGBtoHSL();
  802. findWidget(ID_H_N)->load();
  803. findWidget(ID_S_N)->load();
  804. findWidget(ID_L_N)->load();
  805. findWidget(ID_H_S)->load();
  806. findWidget(ID_S_S)->load();
  807. findWidget(ID_L_S)->load();
  808. findWidget(ID_RGBSELECT)->load();
  809. }
  810. // If HSL updated, copy to RGB
  811. if ((id >= ID_H_N) && (id <= ID_L_S)) {
  812. theColor.HSLtoRGB();
  813. findWidget(ID_R_N)->load();
  814. findWidget(ID_G_N)->load();
  815. findWidget(ID_B_N)->load();
  816. findWidget(ID_R_S)->load();
  817. findWidget(ID_G_S)->load();
  818. findWidget(ID_B_S)->load();
  819. findWidget(ID_RGBSELECT)->load();
  820. }
  821. // Update sample, if present
  822. if (initializedH >= 350) findWidget(ID_DEMOCOLOR)->load();
  823. inSiblingModify = 0;
  824. }
  825. }
  826. }