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

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