compositor for 2d glitch effects
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.

statics_and_lists.pde 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  1. String FX[] = {
  2. "ASDFPIXELSORT",
  3. "DISTORTER",
  4. "FM",
  5. "WZIP",
  6. "AUECHO",
  7. "SLITSCAN",
  8. "WAHWAH",
  9. "PHASER",
  10. "ECHO",
  11. "DARKER",
  12. "BRIGHTER",
  13. "AMPLIFY",
  14. "POSTER",
  15. "DUAL",
  16. "GRAUZONE",
  17. "COPYZOOM",
  18. "SUBTLESORT",
  19. "SCANKER",
  20. "MASK",
  21. "DRAWSTROKES",
  22. "DRAWGENERATIVE",
  23. "PIXELDRIFTER",
  24. "DRIPDRIP",
  25. "WRONGQSORT",
  26. "VHS",
  27. "LZ7",
  28. "LENS",
  29. "SLICER",
  30. "STREAKER",
  31. "SEGMENTER",
  32. "COLORCRUSHER",
  33. "JPGCORRUPTION",
  34. "WEBPCORRUPTION",
  35. "SOXALLPASS",
  36. "SOXBAND",
  37. "SOXBANDPASS",
  38. "SOXBANDREJECT",
  39. "SOXBASS",
  40. "SOXBEND",
  41. "SOXBIQUAD",
  42. "SOXCHORUS",
  43. "SOXCHANNELS",
  44. "SOXCOMPAND",
  45. "SOXCONTRAST",
  46. "SOXDCSHIFT",
  47. "SOXDEEMPH",
  48. "SOXDELAY",
  49. "SOXDITHER",
  50. "SOXDIVIDE",
  51. "SOXDOWNSAMPLE",
  52. "SOXEARWAX",
  53. "SOXECHO",
  54. "SOXECHOS",
  55. "SOXEQUALIZER",
  56. "SOXFADE",
  57. "SOXFIR",
  58. "SOXFIRFIT",
  59. "SOXFLANGER",
  60. "SOXGAIN",
  61. "SOXHIGHPASS",
  62. "SOXHILBERT",
  63. "SOXINPUT",
  64. "SOXLOUDNESS",
  65. "SOXLOWPASS",
  66. "SOXMCOMPAND",
  67. "SOXNOISEPROF",
  68. "SOXNOISERED",
  69. "SOXNORM",
  70. "SOXOOPS",
  71. "SOXOUTPUT",
  72. "SOXOVERDRIVE",
  73. "SOXPAD",
  74. "SOXPHASER",
  75. "SOXPITCH",
  76. "SOXRATE",
  77. "SOXREMIX",
  78. "SOXREPEAT",
  79. "SOXREVERB",
  80. "SOXREVERSE",
  81. "SOXRIAA",
  82. "SOXSILENCE",
  83. "SOXSINC",
  84. "SOXSPECTOGRAM",
  85. "SOXSPEED",
  86. "SOXSPLICE",
  87. "SOXSTAT",
  88. "SOXSTATS",
  89. "SOXSTRETCH",
  90. "SOXSWAP",
  91. "SOXSYNTH",
  92. "SOXTEMPO",
  93. "SOXTREBLE",
  94. "SOXTREMOLO",
  95. "SOXTRIM",
  96. "SOXUPSAMPLE",
  97. "SOXVAD",
  98. "SOXVOL"
  99. };
  100. //parameter return values
  101. final static int INTVAL = 100;
  102. final static int FLOATVAL = 101;
  103. //general parameters
  104. final static int DIRECTION = 201;
  105. final int[] blends = {ADD, SUBTRACT, DARKEST, LIGHTEST, DIFFERENCE, EXCLUSION, MULTIPLY, SCREEN, OVERLAY, HARD_LIGHT, SOFT_LIGHT, DODGE, BURN};
  106. //waveforms for oscillator
  107. final static int SINE = 0; //add 0.25 to phase to get cos
  108. final static int SAWTOOTH = 1;
  109. final static int SAWTOOTHINVERSE = 2;
  110. final static int SQUAR = 3; //switches between -1 and 1, that's all
  111. final static int TRIANG = 4;
  112. final static int TAN = 5;
  113. final static int TANINVERSE = 6;
  114. final static int RAMP = 7; //line for half the sequence up to 1, then stay at 1 for the other half
  115. final static int RAMPINVERSE = 8; //line for half the sequence up to 1, then stay at 1 for the other half
  116. final static int RAMPUPDOWN = 9; //line up, stay, line down, stay
  117. final static int RANDOM = 10;
  118. // LENS TYPES for imagelens
  119. final static int LINEAR = 0;
  120. final static int POWER = 1;
  121. final static int POLAR = 2;
  122. final static int SINUSOIDAL = 3;
  123. // general channels to work with
  124. final static int RED = 0;
  125. final static int GREEN = 1;
  126. final static int BLUE = 2;
  127. final static int HUE = 3;
  128. final static int SATURATION = 4;
  129. final static int BRIGHTNESS = 5;
  130. final static int NRED = 6;
  131. final static int NGREEN = 7;
  132. final static int NBLUE = 8;
  133. final static int NHUE = 9;
  134. final static int NSATURATION = 10;
  135. final static int NBRIGHTNESS = 11;
  136. // FLAGS, ALGORITHMS and EASING mathod by sulej for ultimate sort
  137. //colorspaces from tomasz sulej's FM effect
  138. final static int COLORSPACES = 16;
  139. final static int OHTA = 0;
  140. // RGB == 1; defined in processing
  141. final static int CMY = 2;
  142. // HSB == 3; defined in processing
  143. final static int XYZ = 4;
  144. final static int YXY = 5;
  145. final static int HCL = 6;
  146. final static int LUV = 7;
  147. final static int LAB = 8;
  148. final static int HWB = 9;
  149. final static int RGGBG = 10;
  150. final static int YPbPr = 11;
  151. final static int YCbCr = 12;
  152. final static int YDbDr = 13;
  153. final static int GS = 14;
  154. final static int YUV = 15;
  155. // helper functions and classes
  156. // name
  157. String getColorspaceName(int cs) {
  158. switch(cs) {
  159. case OHTA:
  160. return "OHTA";
  161. case CMY:
  162. return "CMY";
  163. case XYZ:
  164. return "XYZ";
  165. case YXY:
  166. return "YXY";
  167. case HCL:
  168. return "HCL";
  169. case LUV:
  170. return "LUV";
  171. case LAB:
  172. return "LAB";
  173. case HWB:
  174. return "HWB";
  175. case HSB:
  176. return "HSB";
  177. case RGGBG:
  178. return "R-GGB-G";
  179. case YPbPr:
  180. return "YPbPr";
  181. case YCbCr:
  182. return "YCbCr";
  183. case YDbDr:
  184. return "YDbDr";
  185. case GS:
  186. return "Greyscale";
  187. case YUV:
  188. return "YUV";
  189. default:
  190. return "RGB";
  191. }
  192. }
  193. // colorspace converters
  194. color fromColorspace(color c, int cs) {
  195. switch(cs) {
  196. case OHTA:
  197. return fromOHTA(c);
  198. case CMY:
  199. return fromCMY(c);
  200. case XYZ:
  201. return fromXYZ(c);
  202. case YXY:
  203. return fromYXY(c);
  204. case HCL:
  205. return fromHCL(c);
  206. case LUV:
  207. return fromLUV(c);
  208. case LAB:
  209. return fromLAB(c);
  210. case HWB:
  211. return fromHWB(c);
  212. case HSB:
  213. return fromHSB(c);
  214. case RGGBG:
  215. return fromRGGBG(c);
  216. case YPbPr:
  217. return fromYPbPr(c);
  218. case YCbCr:
  219. return fromYCbCr(c);
  220. case YDbDr:
  221. return fromYDbDr(c);
  222. case GS:
  223. return tofromGS(c);
  224. case YUV:
  225. return fromYUV(c);
  226. default:
  227. return c;
  228. }
  229. }
  230. color toColorspace(color c, int cs) {
  231. switch(cs) {
  232. case OHTA:
  233. return toOHTA(c);
  234. case CMY:
  235. return toCMY(c);
  236. case XYZ:
  237. return toXYZ(c);
  238. case YXY:
  239. return toYXY(c);
  240. case HCL:
  241. return toHCL(c);
  242. case LUV:
  243. return toLUV(c);
  244. case LAB:
  245. return toLAB(c);
  246. case HWB:
  247. return toHWB(c);
  248. case HSB:
  249. return toHSB(c);
  250. case RGGBG:
  251. return toRGGBG(c);
  252. case YPbPr:
  253. return toYPbPr(c);
  254. case YCbCr:
  255. return toYCbCr(c);
  256. case YDbDr:
  257. return toYDbDr(c);
  258. case YUV:
  259. return toYUV(c);
  260. case GS:
  261. return tofromGS(c);
  262. default:
  263. return c;
  264. }
  265. }
  266. // Colorspace converters
  267. final int getR(color c) {
  268. return (c & 0xff0000) >> 16;
  269. }
  270. final int getG(color c) {
  271. return (c & 0xff00) >> 8;
  272. }
  273. final int getB(color c) {
  274. return c & 0xff;
  275. }
  276. final int getLuma(color c) {
  277. return constrain((int)(0.2126*getR(c)+0.7152*getG(c)+0.0722*getB(c)), 0, 255);
  278. }
  279. int getChannel(color c, int ch) {
  280. switch(ch) {
  281. case 0 :
  282. return getR(c);
  283. case 1 :
  284. return getG(c);
  285. case 2 :
  286. return getB(c);
  287. default:
  288. return 0;
  289. }
  290. }
  291. // normalized versions
  292. final float getNR(color c) {
  293. return r255[(c & 0xff0000) >> 16];
  294. }
  295. final float getNG(color c) {
  296. return r255[(c & 0xff00) >> 8];
  297. }
  298. final float getNB(color c) {
  299. return r255[c & 0xff];
  300. }
  301. final float getNLuma(color c) {
  302. return r255[getLuma(c)];
  303. }
  304. color blendRGB(color c, int r, int g, int b) {
  305. return (c & 0xff000000) | (constrain(r, 0, 255) << 16) | (constrain(g, 0, 255) << 8 ) | constrain(b, 0, 255);
  306. }
  307. color blendRGB(color c, float r, float g, float b) {
  308. return blendRGB(c, (int)(r*255), (int)(g*255), (int)(b*255));
  309. }
  310. /**************
  311. * Greyscale
  312. **************/
  313. color tofromGS(color c) {
  314. int l = getLuma(c);
  315. return blendRGB(c, l, l, l);
  316. }
  317. /**************
  318. * YUV
  319. **************/
  320. final static float Umax = 0.436 * 255.0;
  321. final static float Vmax = 0.615 * 255.0;
  322. color toYUV(color c) {
  323. int R = getR(c);
  324. int G = getG(c);
  325. int B = getB(c);
  326. int Y = (int)( 0.299*R+0.587*G+0.114*B);
  327. int U = (int)map(-0.14713*R-0.28886*G+0.436*B, -Umax, Umax, 0, 255);
  328. int V = (int)map(0.615*R-0.51499*G-0.10001*B, -Vmax, Vmax, 0, 255);
  329. return blendRGB(c, Y, U, V);
  330. }
  331. color fromYUV(color c) {
  332. int Y = getR(c);
  333. float U = map(getG(c), 0, 255, -Umax, Umax);
  334. float V = map(getB(c), 0, 255, -Vmax, Vmax);
  335. int R = (int)(Y + 1.13983*V);
  336. int G = (int)(Y - 0.39465*U - 0.58060*V);
  337. int B = (int)(Y + 2.03211*U);
  338. return blendRGB(c, R, G, B);
  339. }
  340. /**************
  341. * YDbDr
  342. **************/
  343. color toYDbDr(color c) {
  344. int R = getR(c);
  345. int G = getG(c);
  346. int B = getB(c);
  347. int Y = (int)( 0.299*R+0.587*G+0.114*B);
  348. int Db = (int)(127.5+(-0.450*R-0.883*G+1.333*B)/2.666);
  349. int Dr = (int)(127.5+(-1.333*R+1.116*G+0.217*B)/2.666);
  350. return blendRGB(c, Y, Db, Dr);
  351. }
  352. color fromYDbDr(color c) {
  353. int Y = getR(c);
  354. float Db = (getG(c)-127.5)*2.666;
  355. float Dr = (getB(c)-127.5)*2.666;
  356. int R = (int)(Y + 9.2303716147657e-05*Db-0.52591263066186533*Dr);
  357. int G = (int)(Y - 0.12913289889050927*Db+0.26789932820759876*Dr);
  358. int B = (int)(Y + 0.66467905997895482*Db-7.9202543533108e-05*Dr);
  359. return blendRGB(c, R, G, B);
  360. }
  361. /**************
  362. * YCbCr
  363. **************/
  364. color toYCbCr(color c) {
  365. int R = getR(c);
  366. int G = getG(c);
  367. int B = getB(c);
  368. int Y = (int)( 0.2988390*R+0.5868110*G+0.1143500*B);
  369. int Cb = (int)(-0.168736*R-0.3312640*G+0.5000000*B+127.5);
  370. int Cr = (int)( 0.5000000*R-0.4186880*G-0.0813120*B+127.5);
  371. return blendRGB(c, Y, Cb, Cr);
  372. }
  373. color fromYCbCr(color c) {
  374. int Y = getR(c);
  375. float Cb = getG(c) - 127.5;
  376. float Cr = getB(c) - 127.5;
  377. int R = (int)(Y + 1.402*Cr)+1; // some fix
  378. int G = (int)(Y-0.344136*Cb-0.714136*Cr);
  379. int B = (int)(Y+1.772000*Cb)+1; // some fix
  380. return blendRGB(c, R, G, B);
  381. }
  382. /**************
  383. * YPbPr
  384. **************/
  385. color toYPbPr(color c) {
  386. int R = getR(c);
  387. int B = getB(c);
  388. int Y = getLuma(c);
  389. int Pb = B - Y;
  390. int Pr = R - Y;
  391. if (Pb<0) Pb+=256;
  392. if (Pr<0) Pr+=256;
  393. return blendRGB(c, Y, Pb, Pr);
  394. }
  395. color fromYPbPr(color c) {
  396. int Y = getR(c);
  397. int B = getG(c) + Y;
  398. int R = getB(c) + Y;
  399. if (R>255) R-=256;
  400. if (B>255) B-=256;
  401. int G = (int)((Y-0.2126*R-0.0722*B)/0.7152);
  402. return blendRGB(c, R, G, B);
  403. }
  404. /**************
  405. * R-G,G,B-G
  406. **************/
  407. color toRGGBG(color c) {
  408. int G = getG(c);
  409. int R = getR(c)-G;
  410. int B = getB(c)-G;
  411. if (R<0) R+=256;
  412. if (B<0) B+=256;
  413. return blendRGB(c, R, G, B);
  414. }
  415. color fromRGGBG(color c) {
  416. int G = getG(c);
  417. int R = getR(c)+G;
  418. int B = getB(c)+G;
  419. if (R>255) R-=256;
  420. if (B>255) B-=256;
  421. return blendRGB(c, R, G, B);
  422. }
  423. /**************
  424. * HWB
  425. **************/
  426. color toHSB(color c) {
  427. int R = getR(c);
  428. int G = getG(c);
  429. int B = getB(c);
  430. int _min = min(R, G, B);
  431. int _max = max(R, G, B);
  432. float delta = _max-_min;
  433. float saturation = delta/_max;
  434. float brightness = r255[_max];
  435. if (delta == 0.0) return blendRGB(c, 0.0, saturation, brightness);
  436. float hue = 0;
  437. if (R == _max) hue = (G-B)/delta;
  438. else if (G == _max) hue = 2.0 + (B-R)/delta;
  439. else hue = 4.0 + (R-G)/delta;
  440. hue /= 6.0;
  441. if (hue < 0.0) hue += 1.0;
  442. return blendRGB(c, hue, saturation, brightness);
  443. }
  444. color fromHSB(color c) {
  445. float S = getNG(c);
  446. float B = getNB(c);
  447. if (S == 0.0) return blendRGB(c, B, B, B);
  448. float h = 6.0 * getNR(c);
  449. float f = h-floor(h);
  450. float p = B*(1.0-S);
  451. float q = B*(1.0-S*f);
  452. float t = B*(1.0-(S*(1.0-f)));
  453. float r, g, b;
  454. switch((int)h) {
  455. case 1:
  456. r=q;
  457. g=B;
  458. b=p;
  459. break;
  460. case 2:
  461. r=p;
  462. g=B;
  463. b=t;
  464. break;
  465. case 3:
  466. r=p;
  467. g=q;
  468. b=B;
  469. break;
  470. case 4:
  471. r=t;
  472. g=p;
  473. b=B;
  474. break;
  475. case 5:
  476. r=B;
  477. g=p;
  478. b=q;
  479. break;
  480. default:
  481. r=B;
  482. g=t;
  483. b=p;
  484. break;
  485. }
  486. return blendRGB(c, r, g, b);
  487. }
  488. /**************
  489. * HWB
  490. **************/
  491. color toHWB(color c) {
  492. int R = getR(c);
  493. int G = getG(c);
  494. int B = getB(c);
  495. int w = min(R, G, B);
  496. int v = max(R, G, B);
  497. int hue;
  498. if (v == w) hue = 255;
  499. else {
  500. float f = ((R == w) ? G-B : ((G == w) ? B-R : R-G));
  501. float p = (R == w) ? 3.0 : ((G == w) ? 5.0 : 1.0);
  502. hue = (int)map((p-f/(v-w))/6.0, 0, 1, 0, 254);
  503. }
  504. return blendRGB(c, hue, w, 255-v);
  505. }
  506. color fromHWB(color c) {
  507. int H = getR(c);
  508. int B = 255-getB(c);
  509. if (H == 255) return blendRGB(c, B, B, B);
  510. else {
  511. float hue = map(H, 0, 254, 0, 6);
  512. float v = r255[B];
  513. float whiteness = getNG(c);
  514. int i = (int)floor(hue);
  515. float f = hue-i;
  516. if ((i&0x01)!= 0) f=1.0-f;
  517. float n = whiteness+f*(v-whiteness);
  518. float r, g, b;
  519. switch(i) {
  520. case 1:
  521. r=n;
  522. g=v;
  523. b=whiteness;
  524. break;
  525. case 2:
  526. r=whiteness;
  527. g=v;
  528. b=n;
  529. break;
  530. case 3:
  531. r=whiteness;
  532. g=n;
  533. b=v;
  534. break;
  535. case 4:
  536. r=n;
  537. g=whiteness;
  538. b=v;
  539. break;
  540. case 5:
  541. r=v;
  542. g=whiteness;
  543. b=n;
  544. break;
  545. default:
  546. r=v;
  547. g=n;
  548. b=whiteness;
  549. break;
  550. }
  551. return blendRGB(c, r, g, b);
  552. }
  553. }
  554. /**************
  555. * Lab
  556. **************/
  557. final static float D65X=0.950456;
  558. final static float D65Y=1.0;
  559. final static float D65Z=1.088754;
  560. final static float CIEEpsilon=(216.0/24389.0);
  561. final static float CIEK=(24389.0/27.0);
  562. final static float CIEK2epsilon = CIEK * CIEEpsilon;
  563. final static float D65FX_4 = 4.0*D65X/(D65X+15.0*D65Y+3.0*D65Z);
  564. final static float D65FY_9 = 9.0*D65Y/(D65X+15.0*D65Y+3.0*D65Z);
  565. final static float RANGE_X = 100.0 * (0.4124+0.3576+0.1805);
  566. final static float RANGE_Y = 100.0;
  567. final static float RANGE_Z = 100.0 * (0.0193+0.1192+0.9505);
  568. final static float mepsilon = 1.0e-10;
  569. final static float corrratio = 1.0/2.4;
  570. final static float One_Third = 1.0/3.0;
  571. final static float one_hsixteen = 1.0/116.0;
  572. color toLAB(color c) {
  573. PVector xyz = _toXYZ(getNR(c), getNG(c), getNB(c));
  574. xyz.div(100.0);
  575. xyz.x /= D65X;
  576. xyz.y /= D65Y;
  577. xyz.z /= D65Z;
  578. float x, y, z;
  579. if (xyz.x > CIEEpsilon) {
  580. x = pow(xyz.x, One_Third);
  581. } else {
  582. x= (CIEK*xyz.x+16.0)*one_hsixteen;
  583. }
  584. if (xyz.y > CIEEpsilon) {
  585. y = pow(xyz.y, One_Third);
  586. } else {
  587. y = (CIEK*xyz.y+16.0)*one_hsixteen;
  588. }
  589. if (xyz.z > CIEEpsilon) {
  590. z = pow(xyz.z, One_Third);
  591. } else {
  592. z = (CIEK*xyz.z+16.0)*one_hsixteen;
  593. }
  594. float L = 255.0*(((116.0*y)-16.0)*0.01);
  595. float a = 255.0*(0.5*(x-y)+0.5);
  596. float b = 255.0*(0.5*(y-z)+0.5);
  597. return blendRGB(c, round(L), round(a), round(b));
  598. }
  599. color fromLAB(color c) {
  600. float L = 100*getNR(c);
  601. float a = getNG(c)-0.5;
  602. float b = getNB(c)-0.5;
  603. float y = (L+16.0)*one_hsixteen;
  604. float x = y+a;
  605. float z = y-b;
  606. float xxx=x*x*x;
  607. if (xxx>CIEEpsilon) {
  608. x = xxx;
  609. } else {
  610. x = (116.0*x-16.0)/CIEK;
  611. }
  612. float yyy=y*y*y;
  613. if (yyy>CIEEpsilon) {
  614. y = yyy;
  615. } else {
  616. y = L/CIEK;
  617. }
  618. float zzz=z*z*z;
  619. if (zzz>CIEEpsilon) {
  620. z = zzz;
  621. } else {
  622. z = (116.0*z-16.0)/CIEK;
  623. }
  624. return _fromXYZ(c, RANGE_X*x, RANGE_Y*y, RANGE_Z*z);
  625. }
  626. /**************
  627. * Luv
  628. **************/
  629. final float PerceptibleReciprocal(float x) {
  630. float sgn = x < 0.0 ? -1.0 : 1.0;
  631. if ((sgn * x) >= mepsilon) return (1.0 / x);
  632. return (sgn/mepsilon);
  633. }
  634. color toLUV(color c) {
  635. PVector xyz = _toXYZ(getNR(c), getNG(c), getNB(c));
  636. xyz.div(100.0);
  637. float d = xyz.y; // / D65Y;
  638. float L;
  639. if (d > CIEEpsilon) L = 116.0*pow(d, One_Third)-16.0;
  640. else L = CIEK * d;
  641. float alpha = PerceptibleReciprocal(xyz.x + 15.0 * xyz.y + 3.0 * xyz.z);
  642. float L13 = 13.0 * L;
  643. float u = L13 * ((4.0 * alpha * xyz.x)-D65FX_4);
  644. float v = L13 * ((9.0 * alpha * xyz.y)-D65FY_9);
  645. L /= 100.0;
  646. u=(u+134.0)/354.0;
  647. v=(v+140.0)/262.0;
  648. return blendRGB(c, round(L*255), round(u*255), round(v*255));
  649. }
  650. color fromLUV(color c) {
  651. float L = 100.0*getNR(c);
  652. float u = 354.0*getNG(c)-134.0;
  653. float v = 262.0*getNB(c)-140.0;
  654. float X, Y, Z;
  655. if (L > CIEK2epsilon) Y = pow((L+16.0)*one_hsixteen, 3.0);
  656. else Y = L/CIEK;
  657. float L13 = 13.0*L;
  658. float L52 = 52.0*L;
  659. float Y5 = 5.0*Y;
  660. float L13u = L52/(u+L13*D65FX_4);
  661. X=((Y*((39.0*L/(v+L13*D65FY_9))-5.0))+Y5)/((((L13u)-1.0)/3.0)+One_Third);
  662. Z=(X*(((L13u)-1.0)/3.0))-Y5;
  663. return _fromXYZ(c, 100*X, 100*Y, 100*Z);
  664. }
  665. /**************
  666. * HCL
  667. **************/
  668. color toHCL(color c) {
  669. float r = getNR(c);
  670. float g = getNG(c);
  671. float b = getNB(c);
  672. float max = max(r, max(g, b));
  673. float chr = max - min(r, min(g, b));
  674. float h = 0.0;
  675. if ( chr != 0) {
  676. if (r == max) {
  677. h = ((g-b)/chr+6.0) % 6.0;
  678. } else if (g == max) {
  679. h = (b-r)/chr + 2.0;
  680. } else {
  681. h = (r-g)/chr + 4.0;
  682. }
  683. }
  684. return blendRGB(c, round((h/6.0)*255), round(chr*255), round(255*(0.298839*r+0.586811*g+0.114350*b)));
  685. }
  686. color fromHCL(color c) {
  687. float h = 6.0*getNR(c);
  688. float chr = getNG(c);
  689. float l = getNB(c);
  690. float x = chr*(1.0-abs((h%2.0)-1.0));
  691. float r = 0.0;
  692. float g = 0.0;
  693. float b = 0.0;
  694. if ((0.0 <= h) && (h < 1.0)) {
  695. r=chr;
  696. g=x;
  697. } else if ((1.0 <= h) && (h < 2.0)) {
  698. r=x;
  699. g=chr;
  700. } else if ((2.0 <= h) && (h < 3.0)) {
  701. g=chr;
  702. b=x;
  703. } else if ((3.0 <= h) && (h < 4.0)) {
  704. g=x;
  705. b=chr;
  706. } else if ((4.0 <= h) && (h < 5.0)) {
  707. r=x;
  708. b=chr;
  709. } else {//if ((5.0 <= h) && (h < 6.0)) {
  710. r=chr;
  711. b=x;
  712. }
  713. float m = l - (0.298839*r+0.586811*g+0.114350*b);
  714. return blendRGB(c, round(255*(r+m)), round(255*(g+m)), round(255*(b+m)));
  715. }
  716. /**************
  717. * Yxy
  718. **************/
  719. color toYXY(color c) {
  720. PVector xyz = _toXYZ(getNR(c), getNG(c), getNB(c));
  721. float sum = xyz.x + xyz.y + xyz.z;
  722. float x = xyz.x > 0 ? xyz.x / sum : 0.0;
  723. float y = xyz.y > 0 ? xyz.y / sum : 0.0;
  724. return blendRGB(c,
  725. (int)map(xyz.y, 0, RANGE_Y, 0, 255),
  726. (int)map(x, 0.0, 1.0, 0, 255),
  727. (int)map(y, 0.0, 1.0, 0, 255));
  728. }
  729. color fromYXY(color c) {
  730. float Y = map(getR(c), 0, 255, 0, RANGE_Y);
  731. float x = map(getG(c), 0, 255, 0, 1.0);
  732. float y = map(getB(c), 0, 255, 0, 1.0);
  733. float divy = Y / (y>0 ? y : 1.0e-6);
  734. return _fromXYZ(c, x * divy, Y, (1-x-y)*divy);
  735. }
  736. /**************
  737. * XYZ
  738. **************/
  739. // FIXME: range from 0 to 1
  740. float correctionxyz(float n) {
  741. return (n > 0.04045 ? pow((n + 0.055) / 1.055, 2.4) : n / 12.92) * 100.0;
  742. }
  743. PVector _toXYZ(float rr, float gg, float bb) {
  744. float r = correctionxyz(rr);
  745. float g = correctionxyz(gg);
  746. float b = correctionxyz(bb);
  747. return new PVector(r * 0.4124 + g * 0.3576 + b * 0.1805,
  748. r * 0.2126 + g * 0.7152 + b * 0.0722,
  749. r * 0.0193 + g * 0.1192 + b * 0.9505);
  750. }
  751. color toXYZ(color c) {
  752. PVector xyz = _toXYZ(getNR(c), getNG(c), getNB(c));
  753. return blendRGB(c,
  754. (int)map(xyz.x, 0, RANGE_X, 0, 255),
  755. (int)map(xyz.y, 0, RANGE_Y, 0, 255),
  756. (int)map(xyz.z, 0, RANGE_Z, 0, 255));
  757. }
  758. float recorrectionxyz(float n) {
  759. return n > 0.0031308 ? 1.055 * pow(n, corrratio) - 0.055 : 12.92 * n;
  760. }
  761. // FIXME: range from 0 to 1
  762. color _fromXYZ(color c, float xx, float yy, float zz) {
  763. float x = xx/100.0;
  764. float y = yy/100.0;
  765. float z = zz/100.0;
  766. int r = round(255.0*recorrectionxyz(x * 3.2406 + y * -1.5372 + z * -0.4986));
  767. int g = round(255.0*recorrectionxyz(x * -0.9689 + y * 1.8758 + z * 0.0415));
  768. int b = round(255.0*recorrectionxyz(x * 0.0557 + y * -0.2040 + z * 1.0570));
  769. return blendRGB(c, r, g, b);
  770. }
  771. color fromXYZ(color c) {
  772. float x = map(getR(c), 0, 255, 0, RANGE_X);
  773. float y = map(getG(c), 0, 255, 0, RANGE_Y);
  774. float z = map(getB(c), 0, 255, 0, RANGE_Z);
  775. return _fromXYZ(c, x, y, z);
  776. }
  777. /**************
  778. * CMY
  779. **************/
  780. color toCMY(color c) {
  781. return blendRGB(c, 255-getR(c), 255-getG(c), 255-getB(c));
  782. }
  783. color fromCMY(color c) {
  784. return toCMY(c);
  785. }
  786. /**************
  787. * OHTA
  788. **************/
  789. color fromOHTA(color c) {
  790. int I1 = getR(c);
  791. float I2 = map(getG(c), 0, 255, -127.5, 127.5);
  792. float I3 = map(getB(c), 0, 255, -127.5, 127.5);
  793. int R = (int)(I1+1.00000*I2-0.66668*I3);
  794. int G = (int)(I1+1.33333*I3);
  795. int B = (int)(I1-1.00000*I2-0.66668*I3);
  796. return blendRGB(c, R, G, B);
  797. }
  798. color toOHTA(color c) {
  799. int R = getR(c);
  800. int G = getG(c);
  801. int B = getB(c);
  802. int I1 = (int)(0.33333*R+0.33334*G+0.33333*B);
  803. int I2 = (int)map(0.5*(R-B), -127.5, 127.5, 0, 255);
  804. int I3 = (int)map(-0.25000*R+0.50000*G-0.25000*B, -127.5, 127.5, 0, 255);
  805. return blendRGB(c, I1, I2, I3);
  806. }
  807. ////
  808. // 1/n table for n=0..255 - to speed up color conversions things
  809. final static float[] r255 = {
  810. 0.0, 0.003921569, 0.007843138, 0.011764706, 0.015686275, 0.019607844, 0.023529412, 0.02745098, 0.03137255, 0.03529412, 0.039215688,
  811. 0.043137256, 0.047058824, 0.050980393, 0.05490196, 0.05882353, 0.0627451, 0.06666667, 0.07058824, 0.07450981, 0.078431375, 0.08235294,
  812. 0.08627451, 0.09019608, 0.09411765, 0.09803922, 0.101960786, 0.105882354, 0.10980392, 0.11372549, 0.11764706, 0.12156863, 0.1254902,
  813. 0.12941177, 0.13333334, 0.13725491, 0.14117648, 0.14509805, 0.14901961, 0.15294118, 0.15686275, 0.16078432, 0.16470589, 0.16862746,
  814. 0.17254902, 0.1764706, 0.18039216, 0.18431373, 0.1882353, 0.19215687, 0.19607843, 0.2, 0.20392157, 0.20784314, 0.21176471, 0.21568628,
  815. 0.21960784, 0.22352941, 0.22745098, 0.23137255, 0.23529412, 0.23921569, 0.24313726, 0.24705882, 0.2509804, 0.25490198, 0.25882354,
  816. 0.2627451, 0.26666668, 0.27058825, 0.27450982, 0.2784314, 0.28235295, 0.28627452, 0.2901961, 0.29411766, 0.29803923, 0.3019608, 0.30588236,
  817. 0.30980393, 0.3137255, 0.31764707, 0.32156864, 0.3254902, 0.32941177, 0.33333334, 0.3372549, 0.34117648, 0.34509805, 0.34901962, 0.3529412,
  818. 0.35686275, 0.36078432, 0.3647059, 0.36862746, 0.37254903, 0.3764706, 0.38039216, 0.38431373, 0.3882353, 0.39215687, 0.39607844, 0.4,
  819. 0.40392157, 0.40784314, 0.4117647, 0.41568628, 0.41960785, 0.42352942, 0.42745098, 0.43137255, 0.43529412, 0.4392157, 0.44313726,
  820. 0.44705883, 0.4509804, 0.45490196, 0.45882353, 0.4627451, 0.46666667, 0.47058824, 0.4745098, 0.47843137, 0.48235294, 0.4862745, 0.49019608,
  821. 0.49411765, 0.49803922, 0.5019608, 0.5058824, 0.50980395, 0.5137255, 0.5176471, 0.52156866, 0.5254902, 0.5294118, 0.53333336, 0.5372549,
  822. 0.5411765, 0.54509807, 0.54901963, 0.5529412, 0.5568628, 0.56078434, 0.5647059, 0.5686275, 0.57254905, 0.5764706, 0.5803922, 0.58431375,
  823. 0.5882353, 0.5921569, 0.59607846, 0.6, 0.6039216, 0.60784316, 0.6117647, 0.6156863, 0.61960787, 0.62352943, 0.627451, 0.6313726, 0.63529414,
  824. 0.6392157, 0.6431373, 0.64705884, 0.6509804, 0.654902, 0.65882355, 0.6627451, 0.6666667, 0.67058825, 0.6745098, 0.6784314, 0.68235296,
  825. 0.6862745, 0.6901961, 0.69411767, 0.69803923, 0.7019608, 0.7058824, 0.70980394, 0.7137255, 0.7176471, 0.72156864, 0.7254902, 0.7294118,
  826. 0.73333335, 0.7372549, 0.7411765, 0.74509805, 0.7490196, 0.7529412, 0.75686276, 0.7607843, 0.7647059, 0.76862746, 0.77254903, 0.7764706,
  827. 0.78039217, 0.78431374, 0.7882353, 0.7921569, 0.79607844, 0.8, 0.8039216, 0.80784315, 0.8117647, 0.8156863, 0.81960785, 0.8235294, 0.827451,
  828. 0.83137256, 0.8352941, 0.8392157, 0.84313726, 0.84705883, 0.8509804, 0.85490197, 0.85882354, 0.8627451, 0.8666667, 0.87058824, 0.8745098,
  829. 0.8784314, 0.88235295, 0.8862745, 0.8901961, 0.89411765, 0.8980392, 0.9019608, 0.90588236, 0.9098039, 0.9137255, 0.91764706, 0.92156863,
  830. 0.9254902, 0.92941177, 0.93333334, 0.9372549, 0.9411765, 0.94509804, 0.9490196, 0.9529412, 0.95686275, 0.9607843, 0.9647059, 0.96862745,
  831. 0.972549, 0.9764706, 0.98039216, 0.9843137, 0.9882353, 0.99215686, 0.99607843, 1.0
  832. };