compositor for 2d shader
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. //from tomasz sulej's FM effect
  2. final static int COLORSPACES = 16;
  3. final static int OHTA = 0;
  4. // RGB == 1; defined in processing
  5. final static int CMY = 2;
  6. // HSB == 3; defined in processing
  7. final static int XYZ = 4;
  8. final static int YXY = 5;
  9. final static int HCL = 6;
  10. final static int LUV = 7;
  11. final static int LAB = 8;
  12. final static int HWB = 9;
  13. final static int RGGBG = 10;
  14. final static int YPbPr = 11;
  15. final static int YCbCr = 12;
  16. final static int YDbDr = 13;
  17. final static int GS = 14;
  18. final static int YUV = 15;
  19. // name
  20. String getColorspaceName(int cs) {
  21. switch(cs) {
  22. case OHTA:
  23. return "OHTA";
  24. case CMY:
  25. return "CMY";
  26. case XYZ:
  27. return "XYZ";
  28. case YXY:
  29. return "YXY";
  30. case HCL:
  31. return "HCL";
  32. case LUV:
  33. return "LUV";
  34. case LAB:
  35. return "LAB";
  36. case HWB:
  37. return "HWB";
  38. case HSB:
  39. return "HSB";
  40. case RGGBG:
  41. return "R-GGB-G";
  42. case YPbPr:
  43. return "YPbPr";
  44. case YCbCr:
  45. return "YCbCr";
  46. case YDbDr:
  47. return "YDbDr";
  48. case GS:
  49. return "Greyscale";
  50. case YUV:
  51. return "YUV";
  52. default:
  53. return "RGB";
  54. }
  55. }
  56. // colorspace converters
  57. color fromColorspace(color c, int cs) {
  58. switch(cs) {
  59. case OHTA:
  60. return fromOHTA(c);
  61. case CMY:
  62. return fromCMY(c);
  63. case XYZ:
  64. return fromXYZ(c);
  65. case YXY:
  66. return fromYXY(c);
  67. case HCL:
  68. return fromHCL(c);
  69. case LUV:
  70. return fromLUV(c);
  71. case LAB:
  72. return fromLAB(c);
  73. case HWB:
  74. return fromHWB(c);
  75. case HSB:
  76. return fromHSB(c);
  77. case RGGBG:
  78. return fromRGGBG(c);
  79. case YPbPr:
  80. return fromYPbPr(c);
  81. case YCbCr:
  82. return fromYCbCr(c);
  83. case YDbDr:
  84. return fromYDbDr(c);
  85. case GS:
  86. return tofromGS(c);
  87. case YUV:
  88. return fromYUV(c);
  89. default:
  90. return c;
  91. }
  92. }
  93. color toColorspace(color c, int cs) {
  94. switch(cs) {
  95. case OHTA:
  96. return toOHTA(c);
  97. case CMY:
  98. return toCMY(c);
  99. case XYZ:
  100. return toXYZ(c);
  101. case YXY:
  102. return toYXY(c);
  103. case HCL:
  104. return toHCL(c);
  105. case LUV:
  106. return toLUV(c);
  107. case LAB:
  108. return toLAB(c);
  109. case HWB:
  110. return toHWB(c);
  111. case HSB:
  112. return toHSB(c);
  113. case RGGBG:
  114. return toRGGBG(c);
  115. case YPbPr:
  116. return toYPbPr(c);
  117. case YCbCr:
  118. return toYCbCr(c);
  119. case YDbDr:
  120. return toYDbDr(c);
  121. case YUV:
  122. return toYUV(c);
  123. case GS:
  124. return tofromGS(c);
  125. default:
  126. return c;
  127. }
  128. }
  129. // Colorspace converters
  130. final int getR(color c) {
  131. return (c & 0xff0000) >> 16;
  132. }
  133. final int getG(color c) {
  134. return (c & 0xff00) >> 8;
  135. }
  136. final int getB(color c) {
  137. return c & 0xff;
  138. }
  139. final int getLuma(color c) {
  140. return constrain((int)(0.2126*getR(c)+0.7152*getG(c)+0.0722*getB(c)), 0, 255);
  141. }
  142. int getChannel(color c, int ch) {
  143. switch(ch) {
  144. case 0 :
  145. return getR(c);
  146. case 1 :
  147. return getG(c);
  148. case 2 :
  149. return getB(c);
  150. default:
  151. return 0;
  152. }
  153. }
  154. // normalized versions
  155. final float getNR(color c) {
  156. return r255[(c & 0xff0000) >> 16];
  157. }
  158. final float getNG(color c) {
  159. return r255[(c & 0xff00) >> 8];
  160. }
  161. final float getNB(color c) {
  162. return r255[c & 0xff];
  163. }
  164. final float getNLuma(color c) {
  165. return r255[getLuma(c)];
  166. }
  167. color blendRGB(color c, int r, int g, int b) {
  168. return (c & 0xff000000) | (constrain(r, 0, 255) << 16) | (constrain(g, 0, 255) << 8 ) | constrain(b, 0, 255);
  169. }
  170. color blendRGB(color c, float r, float g, float b) {
  171. return blendRGB(c, (int)(r*255), (int)(g*255), (int)(b*255));
  172. }
  173. /**************
  174. * Greyscale
  175. **************/
  176. color tofromGS(color c) {
  177. int l = getLuma(c);
  178. return blendRGB(c, l, l, l);
  179. }
  180. /**************
  181. * YUV
  182. **************/
  183. final static float Umax = 0.436 * 255.0;
  184. final static float Vmax = 0.615 * 255.0;
  185. color toYUV(color c) {
  186. int R = getR(c);
  187. int G = getG(c);
  188. int B = getB(c);
  189. int Y = (int)( 0.299*R+0.587*G+0.114*B);
  190. int U = (int)map(-0.14713*R-0.28886*G+0.436*B,-Umax,Umax,0,255);
  191. int V = (int)map(0.615*R-0.51499*G-0.10001*B,-Vmax,Vmax,0,255);
  192. return blendRGB(c, Y, U, V);
  193. }
  194. color fromYUV(color c) {
  195. int Y = getR(c);
  196. float U = map(getG(c),0,255,-Umax,Umax);
  197. float V = map(getB(c),0,255,-Vmax,Vmax);
  198. int R = (int)(Y + 1.13983*V);
  199. int G = (int)(Y - 0.39465*U - 0.58060*V);
  200. int B = (int)(Y + 2.03211*U);
  201. return blendRGB(c, R, G, B);
  202. }
  203. /**************
  204. * YDbDr
  205. **************/
  206. color toYDbDr(color c) {
  207. int R = getR(c);
  208. int G = getG(c);
  209. int B = getB(c);
  210. int Y = (int)( 0.299*R+0.587*G+0.114*B);
  211. int Db = (int)(127.5+(-0.450*R-0.883*G+1.333*B)/2.666);
  212. int Dr = (int)(127.5+(-1.333*R+1.116*G+0.217*B)/2.666);
  213. return blendRGB(c, Y, Db, Dr);
  214. }
  215. color fromYDbDr(color c) {
  216. int Y = getR(c);
  217. float Db = (getG(c)-127.5)*2.666;
  218. float Dr = (getB(c)-127.5)*2.666;
  219. int R = (int)(Y + 9.2303716147657e-05*Db-0.52591263066186533*Dr);
  220. int G = (int)(Y - 0.12913289889050927*Db+0.26789932820759876*Dr);
  221. int B = (int)(Y + 0.66467905997895482*Db-7.9202543533108e-05*Dr);
  222. return blendRGB(c, R, G, B);
  223. }
  224. /**************
  225. * YCbCr
  226. **************/
  227. color toYCbCr(color c) {
  228. int R = getR(c);
  229. int G = getG(c);
  230. int B = getB(c);
  231. int Y = (int)( 0.2988390*R+0.5868110*G+0.1143500*B);
  232. int Cb = (int)(-0.168736*R-0.3312640*G+0.5000000*B+127.5);
  233. int Cr = (int)( 0.5000000*R-0.4186880*G-0.0813120*B+127.5);
  234. return blendRGB(c, Y, Cb, Cr);
  235. }
  236. color fromYCbCr(color c) {
  237. int Y = getR(c);
  238. float Cb = getG(c) - 127.5;
  239. float Cr = getB(c) - 127.5;
  240. int R = (int)(Y + 1.402*Cr)+1; // some fix
  241. int G = (int)(Y-0.344136*Cb-0.714136*Cr);
  242. int B = (int)(Y+1.772000*Cb)+1; // some fix
  243. return blendRGB(c, R, G, B);
  244. }
  245. /**************
  246. * YPbPr
  247. **************/
  248. color toYPbPr(color c) {
  249. int R = getR(c);
  250. int B = getB(c);
  251. int Y = getLuma(c);
  252. int Pb = B - Y;
  253. int Pr = R - Y;
  254. if(Pb<0) Pb+=256;
  255. if(Pr<0) Pr+=256;
  256. return blendRGB(c, Y, Pb, Pr);
  257. }
  258. color fromYPbPr(color c) {
  259. int Y = getR(c);
  260. int B = getG(c) + Y;
  261. int R = getB(c) + Y;
  262. if(R>255) R-=256;
  263. if(B>255) B-=256;
  264. int G = (int)((Y-0.2126*R-0.0722*B)/0.7152);
  265. return blendRGB(c, R, G, B);
  266. }
  267. /**************
  268. * R-G,G,B-G
  269. **************/
  270. color toRGGBG(color c) {
  271. int G = getG(c);
  272. int R = getR(c)-G;
  273. int B = getB(c)-G;
  274. if(R<0) R+=256;
  275. if(B<0) B+=256;
  276. return blendRGB(c, R, G, B);
  277. }
  278. color fromRGGBG(color c) {
  279. int G = getG(c);
  280. int R = getR(c)+G;
  281. int B = getB(c)+G;
  282. if(R>255) R-=256;
  283. if(B>255) B-=256;
  284. return blendRGB(c, R, G, B);
  285. }
  286. /**************
  287. * HWB
  288. **************/
  289. color toHSB(color c) {
  290. int R = getR(c);
  291. int G = getG(c);
  292. int B = getB(c);
  293. int _min = min(R, G, B);
  294. int _max = max(R, G, B);
  295. float delta = _max-_min;
  296. float saturation = delta/_max;
  297. float brightness = r255[_max];
  298. if (delta == 0.0) return blendRGB(c, 0.0, saturation, brightness);
  299. float hue = 0;
  300. if (R == _max) hue = (G-B)/delta;
  301. else if (G == _max) hue = 2.0 + (B-R)/delta;
  302. else hue = 4.0 + (R-G)/delta;
  303. hue /= 6.0;
  304. if (hue < 0.0) hue += 1.0;
  305. return blendRGB(c, hue, saturation, brightness);
  306. }
  307. color fromHSB(color c) {
  308. float S = getNG(c);
  309. float B = getNB(c);
  310. if (S == 0.0) return blendRGB(c, B, B, B);
  311. float h = 6.0 * getNR(c);
  312. float f = h-floor(h);
  313. float p = B*(1.0-S);
  314. float q = B*(1.0-S*f);
  315. float t = B*(1.0-(S*(1.0-f)));
  316. float r, g, b;
  317. switch((int)h) {
  318. case 1:
  319. r=q;
  320. g=B;
  321. b=p;
  322. break;
  323. case 2:
  324. r=p;
  325. g=B;
  326. b=t;
  327. break;
  328. case 3:
  329. r=p;
  330. g=q;
  331. b=B;
  332. break;
  333. case 4:
  334. r=t;
  335. g=p;
  336. b=B;
  337. break;
  338. case 5:
  339. r=B;
  340. g=p;
  341. b=q;
  342. break;
  343. default:
  344. r=B;
  345. g=t;
  346. b=p;
  347. break;
  348. }
  349. return blendRGB(c, r, g, b);
  350. }
  351. /**************
  352. * HWB
  353. **************/
  354. color toHWB(color c) {
  355. int R = getR(c);
  356. int G = getG(c);
  357. int B = getB(c);
  358. int w = min(R, G, B);
  359. int v = max(R, G, B);
  360. int hue;
  361. if (v == w) hue = 255;
  362. else {
  363. float f = ((R == w) ? G-B : ((G == w) ? B-R : R-G));
  364. float p = (R == w) ? 3.0 : ((G == w) ? 5.0 : 1.0);
  365. hue = (int)map((p-f/(v-w))/6.0, 0, 1, 0, 254);
  366. }
  367. return blendRGB(c, hue, w, 255-v);
  368. }
  369. color fromHWB(color c) {
  370. int H = getR(c);
  371. int B = 255-getB(c);
  372. if (H == 255) return blendRGB(c, B, B, B);
  373. else {
  374. float hue = map(H, 0, 254, 0, 6);
  375. float v = r255[B];
  376. float whiteness = getNG(c);
  377. int i = (int)floor(hue);
  378. float f = hue-i;
  379. if ((i&0x01)!= 0) f=1.0-f;
  380. float n = whiteness+f*(v-whiteness);
  381. float r, g, b;
  382. switch(i) {
  383. case 1:
  384. r=n;
  385. g=v;
  386. b=whiteness;
  387. break;
  388. case 2:
  389. r=whiteness;
  390. g=v;
  391. b=n;
  392. break;
  393. case 3:
  394. r=whiteness;
  395. g=n;
  396. b=v;
  397. break;
  398. case 4:
  399. r=n;
  400. g=whiteness;
  401. b=v;
  402. break;
  403. case 5:
  404. r=v;
  405. g=whiteness;
  406. b=n;
  407. break;
  408. default:
  409. r=v;
  410. g=n;
  411. b=whiteness;
  412. break;
  413. }
  414. return blendRGB(c, r, g, b);
  415. }
  416. }
  417. /**************
  418. * Lab
  419. **************/
  420. final static float D65X=0.950456;
  421. final static float D65Y=1.0;
  422. final static float D65Z=1.088754;
  423. final static float CIEEpsilon=(216.0/24389.0);
  424. final static float CIEK=(24389.0/27.0);
  425. final static float CIEK2epsilon = CIEK * CIEEpsilon;
  426. final static float D65FX_4 = 4.0*D65X/(D65X+15.0*D65Y+3.0*D65Z);
  427. final static float D65FY_9 = 9.0*D65Y/(D65X+15.0*D65Y+3.0*D65Z);
  428. final static float RANGE_X = 100.0 * (0.4124+0.3576+0.1805);
  429. final static float RANGE_Y = 100.0;
  430. final static float RANGE_Z = 100.0 * (0.0193+0.1192+0.9505);
  431. final static float mepsilon = 1.0e-10;
  432. final static float corrratio = 1.0/2.4;
  433. final static float One_Third = 1.0/3.0;
  434. final static float one_hsixteen = 1.0/116.0;
  435. color toLAB(color c) {
  436. PVector xyz = _toXYZ(getNR(c), getNG(c), getNB(c));
  437. xyz.div(100.0);
  438. xyz.x /= D65X;
  439. xyz.y /= D65Y;
  440. xyz.z /= D65Z;
  441. float x, y, z;
  442. if (xyz.x > CIEEpsilon) {
  443. x = pow(xyz.x, One_Third);
  444. } else {
  445. x= (CIEK*xyz.x+16.0)*one_hsixteen;
  446. }
  447. if (xyz.y > CIEEpsilon) {
  448. y = pow(xyz.y, One_Third);
  449. } else {
  450. y = (CIEK*xyz.y+16.0)*one_hsixteen;
  451. }
  452. if (xyz.z > CIEEpsilon) {
  453. z = pow(xyz.z, One_Third);
  454. } else {
  455. z = (CIEK*xyz.z+16.0)*one_hsixteen;
  456. }
  457. float L = 255.0*(((116.0*y)-16.0)*0.01);
  458. float a = 255.0*(0.5*(x-y)+0.5);
  459. float b = 255.0*(0.5*(y-z)+0.5);
  460. return blendRGB(c, round(L), round(a), round(b));
  461. }
  462. color fromLAB(color c) {
  463. float L = 100*getNR(c);
  464. float a = getNG(c)-0.5;
  465. float b = getNB(c)-0.5;
  466. float y = (L+16.0)*one_hsixteen;
  467. float x = y+a;
  468. float z = y-b;
  469. float xxx=x*x*x;
  470. if (xxx>CIEEpsilon) {
  471. x = xxx;
  472. } else {
  473. x = (116.0*x-16.0)/CIEK;
  474. }
  475. float yyy=y*y*y;
  476. if (yyy>CIEEpsilon) {
  477. y = yyy;
  478. } else {
  479. y = L/CIEK;
  480. }
  481. float zzz=z*z*z;
  482. if (zzz>CIEEpsilon) {
  483. z = zzz;
  484. } else {
  485. z = (116.0*z-16.0)/CIEK;
  486. }
  487. return _fromXYZ(c, RANGE_X*x, RANGE_Y*y, RANGE_Z*z);
  488. }
  489. /**************
  490. * Luv
  491. **************/
  492. final float PerceptibleReciprocal(float x) {
  493. float sgn = x < 0.0 ? -1.0 : 1.0;
  494. if ((sgn * x) >= mepsilon) return (1.0 / x);
  495. return (sgn/mepsilon);
  496. }
  497. color toLUV(color c) {
  498. PVector xyz = _toXYZ(getNR(c), getNG(c), getNB(c));
  499. xyz.div(100.0);
  500. float d = xyz.y; // / D65Y;
  501. float L;
  502. if (d > CIEEpsilon) L = 116.0*pow(d, One_Third)-16.0;
  503. else L = CIEK * d;
  504. float alpha = PerceptibleReciprocal(xyz.x + 15.0 * xyz.y + 3.0 * xyz.z);
  505. float L13 = 13.0 * L;
  506. float u = L13 * ((4.0 * alpha * xyz.x)-D65FX_4);
  507. float v = L13 * ((9.0 * alpha * xyz.y)-D65FY_9);
  508. L /= 100.0;
  509. u=(u+134.0)/354.0;
  510. v=(v+140.0)/262.0;
  511. return blendRGB(c, round(L*255), round(u*255), round(v*255));
  512. }
  513. color fromLUV(color c) {
  514. float L = 100.0*getNR(c);
  515. float u = 354.0*getNG(c)-134.0;
  516. float v = 262.0*getNB(c)-140.0;
  517. float X, Y, Z;
  518. if (L > CIEK2epsilon) Y = pow((L+16.0)*one_hsixteen, 3.0);
  519. else Y = L/CIEK;
  520. float L13 = 13.0*L;
  521. float L52 = 52.0*L;
  522. float Y5 = 5.0*Y;
  523. float L13u = L52/(u+L13*D65FX_4);
  524. X=((Y*((39.0*L/(v+L13*D65FY_9))-5.0))+Y5)/((((L13u)-1.0)/3.0)+One_Third);
  525. Z=(X*(((L13u)-1.0)/3.0))-Y5;
  526. return _fromXYZ(c, 100*X, 100*Y, 100*Z);
  527. }
  528. /**************
  529. * HCL
  530. **************/
  531. color toHCL(color c) {
  532. float r = getNR(c);
  533. float g = getNG(c);
  534. float b = getNB(c);
  535. float max = max(r, max(g, b));
  536. float chr = max - min(r, min(g, b));
  537. float h = 0.0;
  538. if ( chr != 0) {
  539. if (r == max) {
  540. h = ((g-b)/chr+6.0) % 6.0;
  541. } else if (g == max) {
  542. h = (b-r)/chr + 2.0;
  543. } else {
  544. h = (r-g)/chr + 4.0;
  545. }
  546. }
  547. return blendRGB(c, round((h/6.0)*255), round(chr*255), round(255*(0.298839*r+0.586811*g+0.114350*b)));
  548. }
  549. color fromHCL(color c) {
  550. float h = 6.0*getNR(c);
  551. float chr = getNG(c);
  552. float l = getNB(c);
  553. float x = chr*(1.0-abs((h%2.0)-1.0));
  554. float r = 0.0;
  555. float g = 0.0;
  556. float b = 0.0;
  557. if ((0.0 <= h) && (h < 1.0)) {
  558. r=chr;
  559. g=x;
  560. } else if ((1.0 <= h) && (h < 2.0)) {
  561. r=x;
  562. g=chr;
  563. } else if ((2.0 <= h) && (h < 3.0)) {
  564. g=chr;
  565. b=x;
  566. } else if ((3.0 <= h) && (h < 4.0)) {
  567. g=x;
  568. b=chr;
  569. } else if ((4.0 <= h) && (h < 5.0)) {
  570. r=x;
  571. b=chr;
  572. } else {//if ((5.0 <= h) && (h < 6.0)) {
  573. r=chr;
  574. b=x;
  575. }
  576. float m = l - (0.298839*r+0.586811*g+0.114350*b);
  577. return blendRGB(c, round(255*(r+m)), round(255*(g+m)), round(255*(b+m)));
  578. }
  579. /**************
  580. * Yxy
  581. **************/
  582. color toYXY(color c) {
  583. PVector xyz = _toXYZ(getNR(c), getNG(c), getNB(c));
  584. float sum = xyz.x + xyz.y + xyz.z;
  585. float x = xyz.x > 0 ? xyz.x / sum : 0.0;
  586. float y = xyz.y > 0 ? xyz.y / sum : 0.0;
  587. return blendRGB(c,
  588. (int)map(xyz.y, 0, RANGE_Y, 0, 255),
  589. (int)map(x, 0.0, 1.0, 0, 255),
  590. (int)map(y, 0.0, 1.0, 0, 255));
  591. }
  592. color fromYXY(color c) {
  593. float Y = map(getR(c), 0, 255, 0, RANGE_Y);
  594. float x = map(getG(c), 0, 255, 0, 1.0);
  595. float y = map(getB(c), 0, 255, 0, 1.0);
  596. float divy = Y / (y>0 ? y : 1.0e-6);
  597. return _fromXYZ(c, x * divy, Y, (1-x-y)*divy);
  598. }
  599. /**************
  600. * XYZ
  601. **************/
  602. // FIXME: range from 0 to 1
  603. float correctionxyz(float n) {
  604. return (n > 0.04045 ? pow((n + 0.055) / 1.055, 2.4) : n / 12.92) * 100.0;
  605. }
  606. PVector _toXYZ(float rr, float gg, float bb) {
  607. float r = correctionxyz(rr);
  608. float g = correctionxyz(gg);
  609. float b = correctionxyz(bb);
  610. return new PVector(r * 0.4124 + g * 0.3576 + b * 0.1805,
  611. r * 0.2126 + g * 0.7152 + b * 0.0722,
  612. r * 0.0193 + g * 0.1192 + b * 0.9505);
  613. }
  614. color toXYZ(color c) {
  615. PVector xyz = _toXYZ(getNR(c), getNG(c), getNB(c));
  616. return blendRGB(c,
  617. (int)map(xyz.x, 0, RANGE_X, 0, 255),
  618. (int)map(xyz.y, 0, RANGE_Y, 0, 255),
  619. (int)map(xyz.z, 0, RANGE_Z, 0, 255));
  620. }
  621. float recorrectionxyz(float n) {
  622. return n > 0.0031308 ? 1.055 * pow(n, corrratio) - 0.055 : 12.92 * n;
  623. }
  624. // FIXME: range from 0 to 1
  625. color _fromXYZ(color c, float xx, float yy, float zz) {
  626. float x = xx/100.0;
  627. float y = yy/100.0;
  628. float z = zz/100.0;
  629. int r = round(255.0*recorrectionxyz(x * 3.2406 + y * -1.5372 + z * -0.4986));
  630. int g = round(255.0*recorrectionxyz(x * -0.9689 + y * 1.8758 + z * 0.0415));
  631. int b = round(255.0*recorrectionxyz(x * 0.0557 + y * -0.2040 + z * 1.0570));
  632. return blendRGB(c, r, g, b);
  633. }
  634. color fromXYZ(color c) {
  635. float x = map(getR(c), 0, 255, 0, RANGE_X);
  636. float y = map(getG(c), 0, 255, 0, RANGE_Y);
  637. float z = map(getB(c), 0, 255, 0, RANGE_Z);
  638. return _fromXYZ(c, x, y, z);
  639. }
  640. /**************
  641. * CMY
  642. **************/
  643. color toCMY(color c) {
  644. return blendRGB(c, 255-getR(c), 255-getG(c), 255-getB(c));
  645. }
  646. color fromCMY(color c) {
  647. return toCMY(c);
  648. }
  649. /**************
  650. * OHTA
  651. **************/
  652. color fromOHTA(color c) {
  653. int I1 = getR(c);
  654. float I2 = map(getG(c), 0, 255, -127.5, 127.5);
  655. float I3 = map(getB(c), 0, 255, -127.5, 127.5);
  656. int R = (int)(I1+1.00000*I2-0.66668*I3);
  657. int G = (int)(I1+1.33333*I3);
  658. int B = (int)(I1-1.00000*I2-0.66668*I3);
  659. return blendRGB(c, R, G, B);
  660. }
  661. color toOHTA(color c) {
  662. int R = getR(c);
  663. int G = getG(c);
  664. int B = getB(c);
  665. int I1 = (int)(0.33333*R+0.33334*G+0.33333*B);
  666. int I2 = (int)map(0.5*(R-B), -127.5, 127.5, 0, 255);
  667. int I3 = (int)map(-0.25000*R+0.50000*G-0.25000*B, -127.5, 127.5, 0, 255);
  668. return blendRGB(c, I1, I2, I3);
  669. }
  670. ////
  671. // 1/n table for n=0..255 - to speed up color conversions things
  672. final static float[] r255 = {
  673. 0.0, 0.003921569, 0.007843138, 0.011764706, 0.015686275, 0.019607844, 0.023529412, 0.02745098, 0.03137255, 0.03529412, 0.039215688,
  674. 0.043137256, 0.047058824, 0.050980393, 0.05490196, 0.05882353, 0.0627451, 0.06666667, 0.07058824, 0.07450981, 0.078431375, 0.08235294,
  675. 0.08627451, 0.09019608, 0.09411765, 0.09803922, 0.101960786, 0.105882354, 0.10980392, 0.11372549, 0.11764706, 0.12156863, 0.1254902,
  676. 0.12941177, 0.13333334, 0.13725491, 0.14117648, 0.14509805, 0.14901961, 0.15294118, 0.15686275, 0.16078432, 0.16470589, 0.16862746,
  677. 0.17254902, 0.1764706, 0.18039216, 0.18431373, 0.1882353, 0.19215687, 0.19607843, 0.2, 0.20392157, 0.20784314, 0.21176471, 0.21568628,
  678. 0.21960784, 0.22352941, 0.22745098, 0.23137255, 0.23529412, 0.23921569, 0.24313726, 0.24705882, 0.2509804, 0.25490198, 0.25882354,
  679. 0.2627451, 0.26666668, 0.27058825, 0.27450982, 0.2784314, 0.28235295, 0.28627452, 0.2901961, 0.29411766, 0.29803923, 0.3019608, 0.30588236,
  680. 0.30980393, 0.3137255, 0.31764707, 0.32156864, 0.3254902, 0.32941177, 0.33333334, 0.3372549, 0.34117648, 0.34509805, 0.34901962, 0.3529412,
  681. 0.35686275, 0.36078432, 0.3647059, 0.36862746, 0.37254903, 0.3764706, 0.38039216, 0.38431373, 0.3882353, 0.39215687, 0.39607844, 0.4,
  682. 0.40392157, 0.40784314, 0.4117647, 0.41568628, 0.41960785, 0.42352942, 0.42745098, 0.43137255, 0.43529412, 0.4392157, 0.44313726,
  683. 0.44705883, 0.4509804, 0.45490196, 0.45882353, 0.4627451, 0.46666667, 0.47058824, 0.4745098, 0.47843137, 0.48235294, 0.4862745, 0.49019608,
  684. 0.49411765, 0.49803922, 0.5019608, 0.5058824, 0.50980395, 0.5137255, 0.5176471, 0.52156866, 0.5254902, 0.5294118, 0.53333336, 0.5372549,
  685. 0.5411765, 0.54509807, 0.54901963, 0.5529412, 0.5568628, 0.56078434, 0.5647059, 0.5686275, 0.57254905, 0.5764706, 0.5803922, 0.58431375,
  686. 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,
  687. 0.6392157, 0.6431373, 0.64705884, 0.6509804, 0.654902, 0.65882355, 0.6627451, 0.6666667, 0.67058825, 0.6745098, 0.6784314, 0.68235296,
  688. 0.6862745, 0.6901961, 0.69411767, 0.69803923, 0.7019608, 0.7058824, 0.70980394, 0.7137255, 0.7176471, 0.72156864, 0.7254902, 0.7294118,
  689. 0.73333335, 0.7372549, 0.7411765, 0.74509805, 0.7490196, 0.7529412, 0.75686276, 0.7607843, 0.7647059, 0.76862746, 0.77254903, 0.7764706,
  690. 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,
  691. 0.83137256, 0.8352941, 0.8392157, 0.84313726, 0.84705883, 0.8509804, 0.85490197, 0.85882354, 0.8627451, 0.8666667, 0.87058824, 0.8745098,
  692. 0.8784314, 0.88235295, 0.8862745, 0.8901961, 0.89411765, 0.8980392, 0.9019608, 0.90588236, 0.9098039, 0.9137255, 0.91764706, 0.92156863,
  693. 0.9254902, 0.92941177, 0.93333334, 0.9372549, 0.9411765, 0.94509804, 0.9490196, 0.9529412, 0.95686275, 0.9607843, 0.9647059, 0.96862745,
  694. 0.972549, 0.9764706, 0.98039216, 0.9843137, 0.9882353, 0.99215686, 0.99607843, 1.0
  695. };