compositor for 2d glitch effects
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751
  1. /*
  2. ASDFPIXELSORT
  3. */
  4. class ASDFPIXELSORT extends Shader {
  5. ASDFPIXELSORT() {
  6. name = "fxASDFPixelSort";
  7. params.add(new Param("black", INTVAL, -17000000, -2000000, new int[]{SINE, SAWTOOTH, RAMPUPDOWN, TAN, TANINVERSE, TRIANG}));
  8. params.add(new Param("target", INTVAL, 0, 2, new int[]{RANDOM}));
  9. }
  10. int previousMode;
  11. void apply() {
  12. if (previousMode != int(params.get(1).value)) {
  13. if (params.get(1).value == 0) changeParam(0, new Param("black", INTVAL, -17000000, -2000000, new int[]{SINE, SAWTOOTH, RAMPUPDOWN, TAN, TANINVERSE, TRIANG}));
  14. if (params.get(1).value == 1) changeParam(0, new Param("brightness", INTVAL, 0, 200, new int[]{SINE, SAWTOOTH, RAMPUPDOWN, TAN, TANINVERSE, TRIANG}));
  15. if (params.get(1).value == 2) changeParam(0, new Param("white", INTVAL, -15000000, -700000, new int[]{SINE, SAWTOOTH, RAMPUPDOWN, TAN, TANINVERSE, TRIANG}));
  16. }
  17. previousMode = int(params.get(1).value);
  18. row = 0;
  19. column = 0;
  20. renderer.beginDraw();
  21. renderer.colorMode(RGB);
  22. colorMode(RGB);
  23. while (column < renderer.width-1) {
  24. renderer.loadPixels();
  25. sortColumn();
  26. column++;
  27. renderer.updatePixels();
  28. }
  29. while (row < renderer.height-1) {
  30. renderer.loadPixels();
  31. sortRow();
  32. row++;
  33. renderer.updatePixels();
  34. }
  35. renderer.endDraw();
  36. }
  37. int row = 0;
  38. int column = 0;
  39. void sortRow() {
  40. int x = 0;
  41. int y = row;
  42. int xend = 0;
  43. while (xend < renderer.width-1) {
  44. switch((int)params.get(1).value) {
  45. case 0:
  46. x = getFirstNotBlackX(x, y);
  47. xend = getNextBlackX(x, y);
  48. break;
  49. case 1:
  50. x = getFirstBrightX(x, y);
  51. xend = getNextDarkX(x, y);
  52. break;
  53. case 2:
  54. x = getFirstNotWhiteX(x, y);
  55. xend = getNextWhiteX(x, y);
  56. break;
  57. default:
  58. break;
  59. }
  60. if (x < 0) break;
  61. int sortLength = xend-x;
  62. color[] unsorted = new color[sortLength];
  63. color[] sorted = new color[sortLength];
  64. for (int i=0; i<sortLength; i++) {
  65. unsorted[i] = renderer.pixels[x + i + y * renderer.width];
  66. }
  67. sorted = sort(unsorted);
  68. for (int i=0; i<sortLength; i++) {
  69. renderer.pixels[x + i + y * renderer.width] = sorted[i];
  70. }
  71. x = xend+1;
  72. }
  73. }
  74. void sortColumn() {
  75. int x = column;
  76. int y = 0;
  77. int yend = 0;
  78. while (yend < renderer.height-1) {
  79. switch((int)params.get(1).value) {
  80. case 0:
  81. y = getFirstNotBlackY(x, y);
  82. yend = getNextBlackY(x, y);
  83. break;
  84. case 1:
  85. y = getFirstBrightY(x, y);
  86. yend = getNextDarkY(x, y);
  87. break;
  88. case 2:
  89. y = getFirstNotWhiteY(x, y);
  90. yend = getNextWhiteY(x, y);
  91. break;
  92. default:
  93. break;
  94. }
  95. if (y < 0) break;
  96. int sortLength = yend-y;
  97. color[] unsorted = new color[sortLength];
  98. color[] sorted = new color[sortLength];
  99. for (int i=0; i<sortLength; i++) {
  100. unsorted[i] = renderer.pixels[x + (y+i) * renderer.width];
  101. }
  102. sorted = sort(unsorted);
  103. for (int i=0; i<sortLength; i++) {
  104. renderer.pixels[x + (y+i) * renderer.width] = sorted[i];
  105. }
  106. y = yend+1;
  107. }
  108. }
  109. //BLACK
  110. int getFirstNotBlackX(int _x, int _y) {
  111. int x = _x;
  112. int y = _y;
  113. color c;
  114. while ( (c = renderer.pixels[x + y * renderer.width]) < params.get(0).value) {
  115. x++;
  116. if (x >= renderer.width) return -1;
  117. }
  118. return x;
  119. }
  120. int getNextBlackX(int _x, int _y) {
  121. int x = _x+1;
  122. int y = _y;
  123. color c;
  124. while ( (c = renderer.pixels[x + y * renderer.width]) > params.get(0).value) {
  125. x++;
  126. if (x >= renderer.width) return renderer.width-1;
  127. }
  128. return x-1;
  129. }
  130. //BRIGHTNESS
  131. int getFirstBrightX(int _x, int _y) {
  132. int x = _x;
  133. int y = _y;
  134. color c;
  135. while (brightness (c = renderer.pixels[x + y * renderer.width]) < params.get(0).value) {
  136. x++;
  137. if (x >= renderer.width) return -1;
  138. }
  139. return x;
  140. }
  141. int getNextDarkX(int _x, int _y) {
  142. int x = _x+1;
  143. int y = _y;
  144. color c;
  145. while (brightness (c = renderer.pixels[x + y * renderer.width]) > params.get(0).value) {
  146. x++;
  147. if (x >= renderer.width) return renderer.width-1;
  148. }
  149. return x-1;
  150. }
  151. //WHITE
  152. int getFirstNotWhiteX(int _x, int _y) {
  153. int x = _x;
  154. int y = _y;
  155. color c;
  156. while ( (c = renderer.pixels[x + y * renderer.width]) > params.get(0).value) {
  157. x++;
  158. if (x >= renderer.width) return -1;
  159. }
  160. return x;
  161. }
  162. int getNextWhiteX(int _x, int _y) {
  163. int x = _x+1;
  164. int y = _y;
  165. color c;
  166. while ( (c = renderer.pixels[x + y * renderer.width]) < params.get(0).value) {
  167. x++;
  168. if (x >= renderer.width) return renderer.width-1;
  169. }
  170. return x-1;
  171. }
  172. //BLACK
  173. int getFirstNotBlackY(int _x, int _y) {
  174. int x = _x;
  175. int y = _y;
  176. color c;
  177. if (y < renderer.height) {
  178. while ( (c = renderer.pixels[x + y * renderer.width]) < params.get(0).value) {
  179. y++;
  180. if (y >= renderer.height) return -1;
  181. }
  182. }
  183. return y;
  184. }
  185. int getNextBlackY(int _x, int _y) {
  186. int x = _x;
  187. int y = _y+1;
  188. color c;
  189. if (y < renderer.height) {
  190. while ( (c = renderer.pixels[x + y * renderer.width]) > params.get(0).value) {
  191. y++;
  192. if (y >= renderer.height) return renderer.height-1;
  193. }
  194. }
  195. return y-1;
  196. }
  197. //BRIGHTNESS
  198. int getFirstBrightY(int _x, int _y) {
  199. int x = _x;
  200. int y = _y;
  201. color c;
  202. if (y < renderer.height) {
  203. while (brightness (c = renderer.pixels[x + y * renderer.width]) < params.get(0).value) {
  204. y++;
  205. if (y >= renderer.height) return -1;
  206. }
  207. }
  208. return y;
  209. }
  210. int getNextDarkY(int _x, int _y) {
  211. int x = _x;
  212. int y = _y+1;
  213. color c;
  214. if (y < renderer.height) {
  215. while (brightness (c = renderer.pixels[x + y * renderer.width]) > params.get(0).value) {
  216. y++;
  217. if (y >= renderer.height) return renderer.height-1;
  218. }
  219. }
  220. return y-1;
  221. }
  222. //WHITE
  223. int getFirstNotWhiteY(int _x, int _y) {
  224. int x = _x;
  225. int y = _y;
  226. color c;
  227. if (y < renderer.height) {
  228. while ( (c = renderer.pixels[x + y * renderer.width]) > params.get(0).value) {
  229. y++;
  230. if (y >= renderer.height) return -1;
  231. }
  232. }
  233. return y;
  234. }
  235. int getNextWhiteY(int _x, int _y) {
  236. int x = _x;
  237. int y = _y+1;
  238. color c;
  239. if (y < renderer.height) {
  240. while ( (c = renderer.pixels[x + y * renderer.width]) < params.get(0).value) {
  241. y++;
  242. if (y >= renderer.height) return renderer.height-1;
  243. }
  244. }
  245. return y-1;
  246. }
  247. }
  248. /*
  249. DISTORTER
  250. */
  251. class DISTORTER extends Shader {
  252. boolean do_blend = false; // blend image after process
  253. int blend_mode = OVERLAY; // blend type
  254. int channel = BRIGHTNESS; // channel used in processing (R,G,B) or (H,S,B)
  255. float scalex = 0.05; // from 0.01 to 1
  256. float scaley = 0.1; // from 0.01 to 1
  257. boolean shift_hue = true;
  258. float shift_amt = 0.1; // from 0 to 1
  259. PImage buffer;
  260. int[][] distort = new int[2][512];
  261. final static float tick = 1.0/512.0;
  262. int mode = 0;
  263. DISTORTER() {
  264. buffer = createImage(renderer.width, renderer.height, ARGB);
  265. name = "fxDistorter";
  266. params.add(new Param("width", FLOATVAL, 2, buffer.width/4-1, new int[]{SINE, SAWTOOTH, RAMPUPDOWN, TAN, TANINVERSE, TRIANG}));
  267. params.add(new Param("height", FLOATVAL, 2, buffer.height/4-1, new int[]{SINE, SAWTOOTH, RAMPUPDOWN, TAN, TANINVERSE, TRIANG}));
  268. params.add(new Param("do blend", INTVAL, 0, 1, new int[]{RANDOM, SQUAR}));
  269. params.add(new Param("shift hue amount", FLOATVAL, 0, 1, new int[]{SAWTOOTH, SAWTOOTHINVERSE, TAN, TANINVERSE, RAMP, RAMPINVERSE}));
  270. params.add(new Param("scale x", FLOATVAL, 0.01, 1, new int[]{SINE, SAWTOOTH, RAMPUPDOWN, TAN, TANINVERSE, TRIANG}));
  271. params.add(new Param("scale y", FLOATVAL, 0.01, 1, new int[]{SINE, SAWTOOTH, RAMPUPDOWN, TAN, TANINVERSE, TRIANG}));
  272. params.add(new Param("blend mode", INTVAL, 0, blends.length-1, new int[]{SINE, SAWTOOTH, RAMPUPDOWN, TAN, TANINVERSE, TRIANG}));
  273. params.add(new Param("channel", INTVAL, 0, 12, new int[]{SINE, SAWTOOTH, RAMPUPDOWN, TAN, TANINVERSE, TRIANG}));
  274. // channel, blend_mode
  275. //params.add(new Param("height", FLOATVAL, 1, buffer.height/4-1, new int[]{SINE, SAWTOOTH, RAMPUPDOWN, TAN, TANINVERSE, TRIANG}));
  276. // prepare distortion pattern
  277. for (int i=0; i<512; i++) {
  278. distort[0][i] = (int)random(-128, 128);
  279. distort[1][i] = (int)random(-128, 128);
  280. }
  281. }
  282. // ALL Channels, Nxxx stand for negative (255-value)
  283. // channels to work with
  284. final static int RED = 0;
  285. final static int GREEN = 1;
  286. final static int BLUE = 2;
  287. final static int HUE = 3;
  288. final static int SATURATION = 4;
  289. final static int BRIGHTNESS = 5;
  290. final static int NRED = 6;
  291. final static int NGREEN = 7;
  292. final static int NBLUE = 8;
  293. final static int NHUE = 9;
  294. final static int NSATURATION = 10;
  295. final static int NBRIGHTNESS = 11;
  296. void apply() {
  297. println(buffer.height);
  298. buffer = renderer.get();
  299. buffer.resize(renderer.width, renderer.height);
  300. float neww = params.get(0).value;
  301. float newh = params.get(1).value;
  302. do_blend = boolean(int(params.get(2).value));
  303. shift_amt = params.get(3).value;
  304. scalex = params.get(4).value;
  305. scaley = params.get(5).value;
  306. blend_mode = blends[(int)params.get(6).value];
  307. channel = (int)params.get(7).value;
  308. float totalnum = neww+newh;
  309. float times = (totalnum/floor(totalnum/neww));
  310. float offx = (totalnum%neww)/times;
  311. float ratiox = neww/buffer.width;
  312. println(ratiox);
  313. renderer.beginDraw();
  314. renderer.noStroke();
  315. for (int y=0; y<buffer.height; y++) {
  316. float yy = y/(float)buffer.height;
  317. for (int x=0; x<buffer.width; x++) {
  318. float xx = x/(float)buffer.width;
  319. float offy = floor(newh*yy);
  320. float fx = xx*ratiox+offx*offy;
  321. float shift = fx%1.0;
  322. float st = shift/tick;
  323. int no1 = floor(st)%512;
  324. int no2 = ceil(st)%512;
  325. float l = st-(float)no1;
  326. float cx = lerp(distort[0][no1], distort[0][no2], l);
  327. float cy = lerp(distort[1][no1], distort[1][no2], l);
  328. float rx =getChannel(buffer.get(x, y), channel);
  329. int sx = (int)((buffer.width+x+cx*rx*scalex*0.1)%buffer.width);
  330. int sy = (int)((buffer.height+y+cy*scaley)%buffer.height);
  331. color c=buffer.get(sx, sy);
  332. if (shift_hue) {
  333. colorMode(HSB, 255);
  334. c = color((hue(c)+shift_amt*255*noise(newh+y))%255.0, constrain(saturation(c)*1.2, 0, 255), constrain(brightness(c), 0, 255));
  335. colorMode(RGB, 255);
  336. }
  337. // buffer.fill(lerpColor(c,img.get(x,y),0.2));
  338. renderer.fill(c); //wärs nich effizienter die pixelmatrix zu ändern ?
  339. renderer.rect(x, y, 1, 1);
  340. }
  341. }
  342. if (do_blend)
  343. renderer.blend(buffer, 0, 0, buffer.width, buffer.height, 0, 0, renderer.width, renderer.height, blend_mode);
  344. renderer.endDraw();
  345. }
  346. float getChannel(color c, int channel) {
  347. int ch = channel>5?channel-6:channel;
  348. float cc;
  349. switch(ch) {
  350. case RED:
  351. cc = red(c);
  352. break;
  353. case GREEN:
  354. cc = green(c);
  355. break;
  356. case BLUE:
  357. cc = blue(c);
  358. break;
  359. case HUE:
  360. cc = hue(c);
  361. break;
  362. case SATURATION:
  363. cc = saturation(c);
  364. break;
  365. default:
  366. cc= brightness(c);
  367. break;
  368. }
  369. return channel>5?255-cc:cc;
  370. }
  371. }
  372. /*
  373. FM
  374. */
  375. class FM extends Shader {
  376. // configuration
  377. int colorspace = RGB;
  378. int quantval = 30; // 0 - off, less - more glitch, more - more precision
  379. boolean do_blend = true; // blend image after process
  380. int blend_mode = OVERLAY; // blend type
  381. //unused parameters (giers):
  382. final static boolean first_channel_only = false; // for L.. or Y.. colorspaces set true to modulate only luma;
  383. final static boolean lowpass1_on = true; // on/off of first low pass filter
  384. final static boolean lowpass2_on = true; // on/off of second low pass filter
  385. final static boolean lowpass3_on = true; // on/off of third low pass filter
  386. // better don't touch it, lowpass filters are run in cascade
  387. float lowpass1_cutoff = 0.25; // percentage of rate
  388. float lowpass2_cutoff = 0.1;
  389. float lowpass3_cutoff = 0.05;
  390. // working buffer
  391. PGraphics buffer;
  392. // local variables
  393. float min_omega, max_omega;
  394. float min_phase_mult=0.05;
  395. float max_phase_mult=50.0;
  396. LowpassFilter lpf1, lpf2, lpf3;
  397. int[][] pxls;
  398. boolean negate = false;
  399. FM() {
  400. name = "fxFM";
  401. params.add(new Param("do blend", INTVAL, 0, 1, new int[]{RANDOM, SQUAR}));
  402. params.add(new Param ("blend_mode", INTVAL, 0, blends.length-1, new int[]{RANDOM}));
  403. params.add(new Param ("omega", FLOATVAL, 0, 1, new int[]{SINE, SAWTOOTH, TRIANG}));
  404. params.add(new Param ("phase", FLOATVAL, 0, 1, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  405. params.add(new Param ("colorspace", INTVAL, 0, 16, new int[]{RANDOM}));
  406. params.add(new Param ("quant", INTVAL, 0, 40, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  407. buffer = createGraphics(renderer.width, renderer.height);
  408. buffer.beginDraw();
  409. buffer.noStroke();
  410. //buffer.smooth(8);
  411. //buffer.background(0);
  412. // buffer.image(renderer, 0, 0);
  413. buffer.endDraw();
  414. //img.loadPixels();
  415. min_omega = TWO_PI/(0.05*renderer.width);
  416. max_omega = TWO_PI/(300.0*renderer.width);
  417. float rate = 100000.0;
  418. lpf1 = new LowpassFilter(rate, lowpass1_cutoff*rate);
  419. lpf2 = new LowpassFilter(rate, lowpass2_cutoff*rate);
  420. lpf3 = new LowpassFilter(rate, lowpass3_cutoff*rate);
  421. rw = renderer.width;
  422. prepareData();
  423. }
  424. void prepareData() {
  425. pxls = new int[3][renderer.pixels.length];
  426. for (int i=0; i<renderer.pixels.length; i++) {
  427. int cl = toColorspace(renderer.pixels[i], colorspace);
  428. pxls[0][i] = (cl >> 16) & 0xff;
  429. pxls[1][i] = (cl >> 8) & 0xff;
  430. pxls[2][i] = (cl) & 0xff;
  431. }
  432. }
  433. float omega, min_phase, max_phase;
  434. int rw, rh;
  435. void apply() {
  436. do_blend = boolean(int(params.get(0).value));
  437. blend_mode = blends[(int)params.get(1).value];
  438. omega = map(sqrt(params.get(2).value), 0, 1, min_omega, max_omega);
  439. float phase = map(sq(params.get(3).value), 0, 1, min_phase_mult, max_phase_mult);
  440. colorspace = (int)params.get(4).value;
  441. quantval = (int) params.get(5).value;
  442. if (rw != renderer.width || rh != renderer.height) {
  443. rw = renderer.width;
  444. rh = renderer.height;
  445. min_omega = TWO_PI/(0.05*renderer.width);
  446. max_omega = TWO_PI/(300.0*renderer.width);
  447. }
  448. prepareData();
  449. buffer.setSize(renderer.width, renderer.height);
  450. //buffer = renderer.get(0, 0, renderer.width, renderer.height);
  451. max_phase = phase * omega;
  452. min_phase = -max_phase;
  453. processImage();
  454. }
  455. void processImage() {
  456. buffer.beginDraw();
  457. buffer.loadPixels();
  458. int [][] dest_pxls = new int[3][renderer.pixels.length];
  459. if (first_channel_only) {
  460. arrayCopy(pxls[1], dest_pxls[1]);
  461. arrayCopy(pxls[2], dest_pxls[2]);
  462. }
  463. for (int i=0; i< (first_channel_only?1:3); i++) {
  464. for (int y=0; y<renderer.height; y++) {
  465. int off = y * renderer.width;
  466. //reset filters each line
  467. lpf1.resetFilter(map(pxls[i][off], 0, 255, min_phase, max_phase));
  468. lpf2.resetFilter(map(pxls[i][off], 0, 255, min_phase, max_phase));
  469. lpf3.resetFilter(map(pxls[i][off], 0, 255, min_phase, max_phase));
  470. float sig_int = 0; // integral of the signal
  471. float pre_m = 0; // previous value of modulated signal
  472. for (int x=0; x<renderer.width; x++) {
  473. /////////////////////////
  474. // FM part starts here
  475. /////////////////////////
  476. float sig = map(pxls[i][x+off], 0, 255, min_phase, max_phase); // current signal value
  477. sig_int += sig; // current value of signal integral
  478. float m = cos(omega * x + sig_int); // modulate signal
  479. if ( quantval > 0) {
  480. m = map((int)map(m, -1, 1, 0, quantval), 0, quantval, -1, 1); // quantize
  481. }
  482. float dem = abs(m-pre_m); // demodulate signal, derivative
  483. pre_m = m; // remember current value
  484. // lowpass filter chain
  485. if (lowpass1_on) dem = lpf1.lowpass(dem);
  486. if (lowpass2_on) dem = lpf2.lowpass(dem);
  487. if (lowpass3_on) dem = lpf3.lowpass(dem);
  488. // remap signal back to channel value
  489. int v = constrain( (int)map(2*(dem-omega), min_phase, max_phase, 0, 255), 0, 255);
  490. //////////////////////
  491. // FM part ends here
  492. //////////////////////
  493. dest_pxls[i][x+off] = negate?255-v:v;
  494. }
  495. }
  496. }
  497. for (int i=0; i<buffer.pixels.length; i++) {
  498. buffer.pixels[i] = fromColorspace(0xff000000 | (dest_pxls[0][i] << 16) | (dest_pxls[1][i] << 8) | (dest_pxls[2][i]), colorspace);
  499. }
  500. buffer.updatePixels();
  501. if (do_blend)
  502. buffer.blend(renderer, 0, 0, renderer.width, renderer.height, 0, 0, buffer.width, buffer.height, blend_mode);
  503. buffer.endDraw();
  504. renderer.beginDraw();
  505. renderer.image(buffer, 0, 0, renderer.width, renderer.height);
  506. renderer.endDraw();
  507. }
  508. class LowpassFilter {
  509. float alpha;
  510. float prev;
  511. public LowpassFilter(float rate, float hz) {
  512. alpha = 0.0;
  513. prev = 0.0;
  514. setFilter(rate, hz);
  515. }
  516. void setFilter(float rate, float hz) {
  517. float timeInterval = 1.0/rate;
  518. float tau = 1.0 / (hz * TWO_PI);
  519. alpha = timeInterval / (tau + timeInterval);
  520. }
  521. void resetFilter(float val) {
  522. prev = val;
  523. }
  524. void resetFilter() {
  525. resetFilter(0);
  526. }
  527. float lowpass(float sample) {
  528. float stage1 = sample * alpha;
  529. float stage2 = prev - (prev * alpha);
  530. prev = (stage1 + stage2);
  531. return prev;
  532. }
  533. float highpass(float sample) {
  534. return sample - lowpass(sample);
  535. }
  536. }
  537. }
  538. /*
  539. WZIP
  540. */
  541. class WZIP extends Shader {
  542. final float sqrt05 = sqrt(0.5);
  543. float[] raw, raw1, raw2, raw3;
  544. float[] in, w, out;
  545. float[] in1, in2, in3, out1, out2, out3;
  546. int n, n2, s;
  547. float scalingfactorin, scalingfactorout;
  548. PImage img;
  549. String sessionid;
  550. WZIP() {
  551. name = "fxWZIP";
  552. params.add(new Param ("scale", FLOATVAL, 0.1, 1000, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  553. params.add(new Param ("factor in", FLOATVAL, 0.01, 1, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  554. params.add(new Param ("factor out", FLOATVAL, 0.01, 1, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  555. params.add(new Param("hsb/rgb", INTVAL, 0, 1, new int[]{RANDOM, SQUAR}));
  556. params.add(new Param("mode", INTVAL, 0, 1, new int[]{RANDOM, SQUAR}));
  557. sessionid = hex((int)random(0xffff), 4);
  558. img = createImage(renderer.width, renderer.height, ARGB);
  559. img = renderer.get(0, 0, renderer.width, renderer.height);
  560. }
  561. void apply() {
  562. // img = createImage(renderer.width, renderer.height, ARGB);
  563. img.resize(renderer.width, renderer.height);
  564. img = renderer.get(0, 0, renderer.width, renderer.height);
  565. s = img.width*img.height;
  566. raw = new float[s*3];
  567. raw1 = new float[s];
  568. raw2 = new float[s];
  569. raw3 = new float[s];
  570. renderer.beginDraw();
  571. renderer.background(0);
  572. renderer.noStroke();
  573. if (boolean((int)params.get(3).value)) { /////////////////////
  574. renderer.colorMode(HSB, 255);
  575. colorMode(HSB, 255);
  576. } else {
  577. renderer.colorMode(RGB, 255);
  578. colorMode(RGB, 255);
  579. }
  580. scalingfactorin = map(params.get(1).value, 0, 1, 0, params.get(0).value); /////////////////////
  581. scalingfactorout = map(params.get(2).value, 0, 1, 0, params.get(0).value); /////////////////////
  582. int iter=0;
  583. int iter2 = 0;
  584. for (int y=0; y<img.height; y++) {
  585. for (int x=0; x<img.width; x++) {
  586. color c = img.get(x, y);
  587. float r, g, b;
  588. if (boolean((int)params.get(3).value)) { /////////////////////
  589. r = hue(c)>127?hue(c)-256:hue(c);
  590. g = saturation(c)>127?saturation(c)-256:saturation(c);
  591. b = brightness(c)>127?brightness(c)-256:brightness(c);
  592. } else {
  593. r = red(c)>127?red(c)-256:red(c);
  594. g = green(c)>127?green(c)-256:green(c);
  595. b = blue(c)>127?blue(c)-256:blue(c);
  596. }
  597. raw[iter++] = r;
  598. raw[iter++] = g;
  599. raw[iter++] = b;
  600. raw1[iter2] = r;
  601. raw2[iter2] = g;
  602. raw3[iter2] = b;
  603. iter2++;
  604. }
  605. }
  606. n = (int)pow(2, ceil(log(s*3)/log(2)));
  607. n2 = (int)pow(2, ceil(log(s)/log(2)));
  608. in = new float[n];
  609. w = new float[n];
  610. out = new float[n];
  611. out1 = new float[n2];
  612. out2 = new float[n2];
  613. out3 = new float[n2];
  614. in1 = new float[n2];
  615. in2 = new float[n2];
  616. in3 = new float[n2];
  617. arrayCopy(raw, 0, in, 0, raw.length);
  618. for (int i=raw.length; i<n; i++) in[i] = raw[raw.length-1];
  619. arrayCopy(raw1, 0, in1, 0, s);
  620. arrayCopy(raw2, 0, in2, 0, s);
  621. arrayCopy(raw3, 0, in3, 0, s);
  622. for (int i=s; i<n2; i++) {
  623. in1[i] = raw1[s-1];
  624. in2[i] = raw2[s-1];
  625. in3[i] = raw3[s-1];
  626. }
  627. if (boolean((int)params.get(4).value)) option1(); /////////////////////
  628. else option2();
  629. renderer.colorMode(RGB);
  630. colorMode(RGB);
  631. renderer.endDraw();
  632. }
  633. // void printOption() {
  634. // String str1, str2;
  635. // if (do_hsb) {
  636. // str1 = "HSBHSBHSB...";
  637. // str2 = "HHH...SSS...BBB...";
  638. // } else {
  639. // str1 = "RGBRGBRGB...";
  640. // str2 = "RRR...GGG...BBB...";
  641. // }
  642. // if (option1) println("channels combined: " + str1);
  643. // else println("channels separated: " + str2);
  644. // }
  645. //
  646. // void printScale() {
  647. // println("Scale: 0.."+sc);
  648. // }
  649. float clamp(float c) {
  650. return(abs(c<0?256+c:c)%255.0);
  651. }
  652. void option2() {
  653. wtrafo(in1, n2);
  654. wbtrafo(out1, n2);
  655. wtrafo(in2, n2);
  656. wbtrafo(out2, n2);
  657. wtrafo(in3, n2);
  658. wbtrafo(out3, n2);
  659. for (int i=0; i<s; i++) {
  660. float r = clamp(out1[i]);
  661. float g = clamp(out2[i]);
  662. float b = clamp(out3[i]);
  663. renderer.fill(r, g, b);
  664. renderer.rect(i%img.width, i/img.width, 1, 1);
  665. }
  666. }
  667. void option1() {
  668. wtrafo(in, n);
  669. wbtrafo(out, n);
  670. float r=0, g=0, b=0;
  671. int state = 0;
  672. for (int i=0; i<raw.length; i++) {
  673. float c = clamp(out[i]);
  674. switch(state) {
  675. case 0:
  676. r = c;
  677. break;
  678. case 1:
  679. g = c;
  680. break;
  681. case 2:
  682. b = c;
  683. break;
  684. default:
  685. {
  686. r = c;
  687. renderer.fill(r, g, b);
  688. renderer.rect(floor(i/3.0)%img.width, floor(i/3.0)/img.width, 1, 1);
  689. state = 0;
  690. }
  691. }
  692. state++;
  693. }
  694. }
  695. void wbtrafo(float[] y, int n) {
  696. float[] d = new float[n];
  697. d[n-2] = w[n-1];
  698. int b1 = n-4;
  699. int b2 = n-2;
  700. int a=1;
  701. while (a<n/2) {
  702. for (int i=0; i<a; i++) {
  703. d[2*i+b1]=(d[i+b2]+w[i+b2])*sqrt05;
  704. d[2*i+1+b1]=(d[i+b2]-w[i+b2])*sqrt05;
  705. }
  706. b2=b1;
  707. b1=b1-4*a;
  708. a*=2;
  709. }
  710. for (int i=0; i<a; i++) {
  711. y[2*i]=(d[i]+w[i])*sqrt05;
  712. y[2*i+1]=(d[i]-w[i])*sqrt05;
  713. }
  714. for (int i=0; i<n; i++) y[i] *= scalingfactorout;
  715. }
  716. void wtrafo(float[] y, int n) {
  717. float[] d = new float[n];
  718. int a = n/2;
  719. for (int i=0; i<a; i++) {
  720. w[i] = (y[2*i]-y[2*i+1])*sqrt05;
  721. d[i] = (y[2*i]+y[2*i+1])*sqrt05;
  722. }
  723. int b1 = 0;
  724. int b2 = a;
  725. a/=2;
  726. while (a>0) {
  727. for (int i=0; i<a; i++) {
  728. w[i+b2]=(d[2*i+b1]-d[2*i+1+b1])*sqrt05;
  729. d[i+b2]=(d[2*i+b1]+d[2*i+1+b1])*sqrt05;
  730. }
  731. b1=b2;
  732. b2=b2+a;
  733. a/=2;
  734. }
  735. w[b2] = d[b1];
  736. for (int i=0; i<n-1; i++) w[i] = (int)(w[i]/scalingfactorin);
  737. if (w[n-1]>0) w[n-1] = (int)(w[n-1]/scalingfactorin+0.5);
  738. else w[n-1] = (int)(w[n-1]/scalingfactorin-0.5);
  739. }
  740. }
  741. // name
  742. String getColorspaceName(int cs) {
  743. switch(cs) {
  744. case OHTA:
  745. return "OHTA";
  746. case CMY:
  747. return "CMY";
  748. case XYZ:
  749. return "XYZ";
  750. case YXY:
  751. return "YXY";
  752. case HCL:
  753. return "HCL";
  754. case LUV:
  755. return "LUV";
  756. case LAB:
  757. return "LAB";
  758. case HWB:
  759. return "HWB";
  760. case HSB:
  761. return "HSB";
  762. case RGGBG:
  763. return "R-GGB-G";
  764. case YPbPr:
  765. return "YPbPr";
  766. case YCbCr:
  767. return "YCbCr";
  768. case YDbDr:
  769. return "YDbDr";
  770. case GS:
  771. return "Greyscale";
  772. case YUV:
  773. return "YUV";
  774. default:
  775. return "RGB";
  776. }
  777. }
  778. // colorspace converters
  779. color fromColorspace(color c, int cs) {
  780. switch(cs) {
  781. case OHTA:
  782. return fromOHTA(c);
  783. case CMY:
  784. return fromCMY(c);
  785. case XYZ:
  786. return fromXYZ(c);
  787. case YXY:
  788. return fromYXY(c);
  789. case HCL:
  790. return fromHCL(c);
  791. case LUV:
  792. return fromLUV(c);
  793. case LAB:
  794. return fromLAB(c);
  795. case HWB:
  796. return fromHWB(c);
  797. case HSB:
  798. return fromHSB(c);
  799. case RGGBG:
  800. return fromRGGBG(c);
  801. case YPbPr:
  802. return fromYPbPr(c);
  803. case YCbCr:
  804. return fromYCbCr(c);
  805. case YDbDr:
  806. return fromYDbDr(c);
  807. case GS:
  808. return tofromGS(c);
  809. case YUV:
  810. return fromYUV(c);
  811. default:
  812. return c;
  813. }
  814. }
  815. color toColorspace(color c, int cs) {
  816. switch(cs) {
  817. case OHTA:
  818. return toOHTA(c);
  819. case CMY:
  820. return toCMY(c);
  821. case XYZ:
  822. return toXYZ(c);
  823. case YXY:
  824. return toYXY(c);
  825. case HCL:
  826. return toHCL(c);
  827. case LUV:
  828. return toLUV(c);
  829. case LAB:
  830. return toLAB(c);
  831. case HWB:
  832. return toHWB(c);
  833. case HSB:
  834. return toHSB(c);
  835. case RGGBG:
  836. return toRGGBG(c);
  837. case YPbPr:
  838. return toYPbPr(c);
  839. case YCbCr:
  840. return toYCbCr(c);
  841. case YDbDr:
  842. return toYDbDr(c);
  843. case YUV:
  844. return toYUV(c);
  845. case GS:
  846. return tofromGS(c);
  847. default:
  848. return c;
  849. }
  850. }
  851. // Colorspace converters
  852. final int getR(color c) {
  853. return (c & 0xff0000) >> 16;
  854. }
  855. final int getG(color c) {
  856. return (c & 0xff00) >> 8;
  857. }
  858. final int getB(color c) {
  859. return c & 0xff;
  860. }
  861. final int getLuma(color c) {
  862. return constrain((int)(0.2126*getR(c)+0.7152*getG(c)+0.0722*getB(c)), 0, 255);
  863. }
  864. int getChannel(color c, int ch) {
  865. switch(ch) {
  866. case 0 :
  867. return getR(c);
  868. case 1 :
  869. return getG(c);
  870. case 2 :
  871. return getB(c);
  872. default:
  873. return 0;
  874. }
  875. }
  876. // normalized versions
  877. final float getNR(color c) {
  878. return r255[(c & 0xff0000) >> 16];
  879. }
  880. final float getNG(color c) {
  881. return r255[(c & 0xff00) >> 8];
  882. }
  883. final float getNB(color c) {
  884. return r255[c & 0xff];
  885. }
  886. final float getNLuma(color c) {
  887. return r255[getLuma(c)];
  888. }
  889. color blendRGB(color c, int r, int g, int b) {
  890. return (c & 0xff000000) | (constrain(r, 0, 255) << 16) | (constrain(g, 0, 255) << 8 ) | constrain(b, 0, 255);
  891. }
  892. color blendRGB(color c, float r, float g, float b) {
  893. return blendRGB(c, (int)(r*255), (int)(g*255), (int)(b*255));
  894. }
  895. /**************
  896. * Greyscale
  897. **************/
  898. color tofromGS(color c) {
  899. int l = getLuma(c);
  900. return blendRGB(c, l, l, l);
  901. }
  902. /**************
  903. * YUV
  904. **************/
  905. final static float Umax = 0.436 * 255.0;
  906. final static float Vmax = 0.615 * 255.0;
  907. color toYUV(color c) {
  908. int R = getR(c);
  909. int G = getG(c);
  910. int B = getB(c);
  911. int Y = (int)( 0.299*R+0.587*G+0.114*B);
  912. int U = (int)map(-0.14713*R-0.28886*G+0.436*B,-Umax,Umax,0,255);
  913. int V = (int)map(0.615*R-0.51499*G-0.10001*B,-Vmax,Vmax,0,255);
  914. return blendRGB(c, Y, U, V);
  915. }
  916. color fromYUV(color c) {
  917. int Y = getR(c);
  918. float U = map(getG(c),0,255,-Umax,Umax);
  919. float V = map(getB(c),0,255,-Vmax,Vmax);
  920. int R = (int)(Y + 1.13983*V);
  921. int G = (int)(Y - 0.39465*U - 0.58060*V);
  922. int B = (int)(Y + 2.03211*U);
  923. return blendRGB(c, R, G, B);
  924. }
  925. /**************
  926. * YDbDr
  927. **************/
  928. color toYDbDr(color c) {
  929. int R = getR(c);
  930. int G = getG(c);
  931. int B = getB(c);
  932. int Y = (int)( 0.299*R+0.587*G+0.114*B);
  933. int Db = (int)(127.5+(-0.450*R-0.883*G+1.333*B)/2.666);
  934. int Dr = (int)(127.5+(-1.333*R+1.116*G+0.217*B)/2.666);
  935. return blendRGB(c, Y, Db, Dr);
  936. }
  937. color fromYDbDr(color c) {
  938. int Y = getR(c);
  939. float Db = (getG(c)-127.5)*2.666;
  940. float Dr = (getB(c)-127.5)*2.666;
  941. int R = (int)(Y + 9.2303716147657e-05*Db-0.52591263066186533*Dr);
  942. int G = (int)(Y - 0.12913289889050927*Db+0.26789932820759876*Dr);
  943. int B = (int)(Y + 0.66467905997895482*Db-7.9202543533108e-05*Dr);
  944. return blendRGB(c, R, G, B);
  945. }
  946. /**************
  947. * YCbCr
  948. **************/
  949. color toYCbCr(color c) {
  950. int R = getR(c);
  951. int G = getG(c);
  952. int B = getB(c);
  953. int Y = (int)( 0.2988390*R+0.5868110*G+0.1143500*B);
  954. int Cb = (int)(-0.168736*R-0.3312640*G+0.5000000*B+127.5);
  955. int Cr = (int)( 0.5000000*R-0.4186880*G-0.0813120*B+127.5);
  956. return blendRGB(c, Y, Cb, Cr);
  957. }
  958. color fromYCbCr(color c) {
  959. int Y = getR(c);
  960. float Cb = getG(c) - 127.5;
  961. float Cr = getB(c) - 127.5;
  962. int R = (int)(Y + 1.402*Cr)+1; // some fix
  963. int G = (int)(Y-0.344136*Cb-0.714136*Cr);
  964. int B = (int)(Y+1.772000*Cb)+1; // some fix
  965. return blendRGB(c, R, G, B);
  966. }
  967. /**************
  968. * YPbPr
  969. **************/
  970. color toYPbPr(color c) {
  971. int R = getR(c);
  972. int B = getB(c);
  973. int Y = getLuma(c);
  974. int Pb = B - Y;
  975. int Pr = R - Y;
  976. if(Pb<0) Pb+=256;
  977. if(Pr<0) Pr+=256;
  978. return blendRGB(c, Y, Pb, Pr);
  979. }
  980. color fromYPbPr(color c) {
  981. int Y = getR(c);
  982. int B = getG(c) + Y;
  983. int R = getB(c) + Y;
  984. if(R>255) R-=256;
  985. if(B>255) B-=256;
  986. int G = (int)((Y-0.2126*R-0.0722*B)/0.7152);
  987. return blendRGB(c, R, G, B);
  988. }
  989. /**************
  990. * R-G,G,B-G
  991. **************/
  992. color toRGGBG(color c) {
  993. int G = getG(c);
  994. int R = getR(c)-G;
  995. int B = getB(c)-G;
  996. if(R<0) R+=256;
  997. if(B<0) B+=256;
  998. return blendRGB(c, R, G, B);
  999. }
  1000. color fromRGGBG(color c) {
  1001. int G = getG(c);
  1002. int R = getR(c)+G;
  1003. int B = getB(c)+G;
  1004. if(R>255) R-=256;
  1005. if(B>255) B-=256;
  1006. return blendRGB(c, R, G, B);
  1007. }
  1008. /**************
  1009. * HWB
  1010. **************/
  1011. color toHSB(color c) {
  1012. int R = getR(c);
  1013. int G = getG(c);
  1014. int B = getB(c);
  1015. int _min = min(R, G, B);
  1016. int _max = max(R, G, B);
  1017. float delta = _max-_min;
  1018. float saturation = delta/_max;
  1019. float brightness = r255[_max];
  1020. if (delta == 0.0) return blendRGB(c, 0.0, saturation, brightness);
  1021. float hue = 0;
  1022. if (R == _max) hue = (G-B)/delta;
  1023. else if (G == _max) hue = 2.0 + (B-R)/delta;
  1024. else hue = 4.0 + (R-G)/delta;
  1025. hue /= 6.0;
  1026. if (hue < 0.0) hue += 1.0;
  1027. return blendRGB(c, hue, saturation, brightness);
  1028. }
  1029. color fromHSB(color c) {
  1030. float S = getNG(c);
  1031. float B = getNB(c);
  1032. if (S == 0.0) return blendRGB(c, B, B, B);
  1033. float h = 6.0 * getNR(c);
  1034. float f = h-floor(h);
  1035. float p = B*(1.0-S);
  1036. float q = B*(1.0-S*f);
  1037. float t = B*(1.0-(S*(1.0-f)));
  1038. float r, g, b;
  1039. switch((int)h) {
  1040. case 1:
  1041. r=q;
  1042. g=B;
  1043. b=p;
  1044. break;
  1045. case 2:
  1046. r=p;
  1047. g=B;
  1048. b=t;
  1049. break;
  1050. case 3:
  1051. r=p;
  1052. g=q;
  1053. b=B;
  1054. break;
  1055. case 4:
  1056. r=t;
  1057. g=p;
  1058. b=B;
  1059. break;
  1060. case 5:
  1061. r=B;
  1062. g=p;
  1063. b=q;
  1064. break;
  1065. default:
  1066. r=B;
  1067. g=t;
  1068. b=p;
  1069. break;
  1070. }
  1071. return blendRGB(c, r, g, b);
  1072. }
  1073. /**************
  1074. * HWB
  1075. **************/
  1076. color toHWB(color c) {
  1077. int R = getR(c);
  1078. int G = getG(c);
  1079. int B = getB(c);
  1080. int w = min(R, G, B);
  1081. int v = max(R, G, B);
  1082. int hue;
  1083. if (v == w) hue = 255;
  1084. else {
  1085. float f = ((R == w) ? G-B : ((G == w) ? B-R : R-G));
  1086. float p = (R == w) ? 3.0 : ((G == w) ? 5.0 : 1.0);
  1087. hue = (int)map((p-f/(v-w))/6.0, 0, 1, 0, 254);
  1088. }
  1089. return blendRGB(c, hue, w, 255-v);
  1090. }
  1091. color fromHWB(color c) {
  1092. int H = getR(c);
  1093. int B = 255-getB(c);
  1094. if (H == 255) return blendRGB(c, B, B, B);
  1095. else {
  1096. float hue = map(H, 0, 254, 0, 6);
  1097. float v = r255[B];
  1098. float whiteness = getNG(c);
  1099. int i = (int)floor(hue);
  1100. float f = hue-i;
  1101. if ((i&0x01)!= 0) f=1.0-f;
  1102. float n = whiteness+f*(v-whiteness);
  1103. float r, g, b;
  1104. switch(i) {
  1105. case 1:
  1106. r=n;
  1107. g=v;
  1108. b=whiteness;
  1109. break;
  1110. case 2:
  1111. r=whiteness;
  1112. g=v;
  1113. b=n;
  1114. break;
  1115. case 3:
  1116. r=whiteness;
  1117. g=n;
  1118. b=v;
  1119. break;
  1120. case 4:
  1121. r=n;
  1122. g=whiteness;
  1123. b=v;
  1124. break;
  1125. case 5:
  1126. r=v;
  1127. g=whiteness;
  1128. b=n;
  1129. break;
  1130. default:
  1131. r=v;
  1132. g=n;
  1133. b=whiteness;
  1134. break;
  1135. }
  1136. return blendRGB(c, r, g, b);
  1137. }
  1138. }
  1139. /**************
  1140. * Lab
  1141. **************/
  1142. final static float D65X=0.950456;
  1143. final static float D65Y=1.0;
  1144. final static float D65Z=1.088754;
  1145. final static float CIEEpsilon=(216.0/24389.0);
  1146. final static float CIEK=(24389.0/27.0);
  1147. final static float CIEK2epsilon = CIEK * CIEEpsilon;
  1148. final static float D65FX_4 = 4.0*D65X/(D65X+15.0*D65Y+3.0*D65Z);
  1149. final static float D65FY_9 = 9.0*D65Y/(D65X+15.0*D65Y+3.0*D65Z);
  1150. final static float RANGE_X = 100.0 * (0.4124+0.3576+0.1805);
  1151. final static float RANGE_Y = 100.0;
  1152. final static float RANGE_Z = 100.0 * (0.0193+0.1192+0.9505);
  1153. final static float mepsilon = 1.0e-10;
  1154. final static float corrratio = 1.0/2.4;
  1155. final static float One_Third = 1.0/3.0;
  1156. final static float one_hsixteen = 1.0/116.0;
  1157. color toLAB(color c) {
  1158. PVector xyz = _toXYZ(getNR(c), getNG(c), getNB(c));
  1159. xyz.div(100.0);
  1160. xyz.x /= D65X;
  1161. xyz.y /= D65Y;
  1162. xyz.z /= D65Z;
  1163. float x, y, z;
  1164. if (xyz.x > CIEEpsilon) {
  1165. x = pow(xyz.x, One_Third);
  1166. } else {
  1167. x= (CIEK*xyz.x+16.0)*one_hsixteen;
  1168. }
  1169. if (xyz.y > CIEEpsilon) {
  1170. y = pow(xyz.y, One_Third);
  1171. } else {
  1172. y = (CIEK*xyz.y+16.0)*one_hsixteen;
  1173. }
  1174. if (xyz.z > CIEEpsilon) {
  1175. z = pow(xyz.z, One_Third);
  1176. } else {
  1177. z = (CIEK*xyz.z+16.0)*one_hsixteen;
  1178. }
  1179. float L = 255.0*(((116.0*y)-16.0)*0.01);
  1180. float a = 255.0*(0.5*(x-y)+0.5);
  1181. float b = 255.0*(0.5*(y-z)+0.5);
  1182. return blendRGB(c, round(L), round(a), round(b));
  1183. }
  1184. color fromLAB(color c) {
  1185. float L = 100*getNR(c);
  1186. float a = getNG(c)-0.5;
  1187. float b = getNB(c)-0.5;
  1188. float y = (L+16.0)*one_hsixteen;
  1189. float x = y+a;
  1190. float z = y-b;
  1191. float xxx=x*x*x;
  1192. if (xxx>CIEEpsilon) {
  1193. x = xxx;
  1194. } else {
  1195. x = (116.0*x-16.0)/CIEK;
  1196. }
  1197. float yyy=y*y*y;
  1198. if (yyy>CIEEpsilon) {
  1199. y = yyy;
  1200. } else {
  1201. y = L/CIEK;
  1202. }
  1203. float zzz=z*z*z;
  1204. if (zzz>CIEEpsilon) {
  1205. z = zzz;
  1206. } else {
  1207. z = (116.0*z-16.0)/CIEK;
  1208. }
  1209. return _fromXYZ(c, RANGE_X*x, RANGE_Y*y, RANGE_Z*z);
  1210. }
  1211. /**************
  1212. * Luv
  1213. **************/
  1214. final float PerceptibleReciprocal(float x) {
  1215. float sgn = x < 0.0 ? -1.0 : 1.0;
  1216. if ((sgn * x) >= mepsilon) return (1.0 / x);
  1217. return (sgn/mepsilon);
  1218. }
  1219. color toLUV(color c) {
  1220. PVector xyz = _toXYZ(getNR(c), getNG(c), getNB(c));
  1221. xyz.div(100.0);
  1222. float d = xyz.y; // / D65Y;
  1223. float L;
  1224. if (d > CIEEpsilon) L = 116.0*pow(d, One_Third)-16.0;
  1225. else L = CIEK * d;
  1226. float alpha = PerceptibleReciprocal(xyz.x + 15.0 * xyz.y + 3.0 * xyz.z);
  1227. float L13 = 13.0 * L;
  1228. float u = L13 * ((4.0 * alpha * xyz.x)-D65FX_4);
  1229. float v = L13 * ((9.0 * alpha * xyz.y)-D65FY_9);
  1230. L /= 100.0;
  1231. u=(u+134.0)/354.0;
  1232. v=(v+140.0)/262.0;
  1233. return blendRGB(c, round(L*255), round(u*255), round(v*255));
  1234. }
  1235. color fromLUV(color c) {
  1236. float L = 100.0*getNR(c);
  1237. float u = 354.0*getNG(c)-134.0;
  1238. float v = 262.0*getNB(c)-140.0;
  1239. float X, Y, Z;
  1240. if (L > CIEK2epsilon) Y = pow((L+16.0)*one_hsixteen, 3.0);
  1241. else Y = L/CIEK;
  1242. float L13 = 13.0*L;
  1243. float L52 = 52.0*L;
  1244. float Y5 = 5.0*Y;
  1245. float L13u = L52/(u+L13*D65FX_4);
  1246. X=((Y*((39.0*L/(v+L13*D65FY_9))-5.0))+Y5)/((((L13u)-1.0)/3.0)+One_Third);
  1247. Z=(X*(((L13u)-1.0)/3.0))-Y5;
  1248. return _fromXYZ(c, 100*X, 100*Y, 100*Z);
  1249. }
  1250. /**************
  1251. * HCL
  1252. **************/
  1253. color toHCL(color c) {
  1254. float r = getNR(c);
  1255. float g = getNG(c);
  1256. float b = getNB(c);
  1257. float max = max(r, max(g, b));
  1258. float chr = max - min(r, min(g, b));
  1259. float h = 0.0;
  1260. if ( chr != 0) {
  1261. if (r == max) {
  1262. h = ((g-b)/chr+6.0) % 6.0;
  1263. } else if (g == max) {
  1264. h = (b-r)/chr + 2.0;
  1265. } else {
  1266. h = (r-g)/chr + 4.0;
  1267. }
  1268. }
  1269. return blendRGB(c, round((h/6.0)*255), round(chr*255), round(255*(0.298839*r+0.586811*g+0.114350*b)));
  1270. }
  1271. color fromHCL(color c) {
  1272. float h = 6.0*getNR(c);
  1273. float chr = getNG(c);
  1274. float l = getNB(c);
  1275. float x = chr*(1.0-abs((h%2.0)-1.0));
  1276. float r = 0.0;
  1277. float g = 0.0;
  1278. float b = 0.0;
  1279. if ((0.0 <= h) && (h < 1.0)) {
  1280. r=chr;
  1281. g=x;
  1282. } else if ((1.0 <= h) && (h < 2.0)) {
  1283. r=x;
  1284. g=chr;
  1285. } else if ((2.0 <= h) && (h < 3.0)) {
  1286. g=chr;
  1287. b=x;
  1288. } else if ((3.0 <= h) && (h < 4.0)) {
  1289. g=x;
  1290. b=chr;
  1291. } else if ((4.0 <= h) && (h < 5.0)) {
  1292. r=x;
  1293. b=chr;
  1294. } else {//if ((5.0 <= h) && (h < 6.0)) {
  1295. r=chr;
  1296. b=x;
  1297. }
  1298. float m = l - (0.298839*r+0.586811*g+0.114350*b);
  1299. return blendRGB(c, round(255*(r+m)), round(255*(g+m)), round(255*(b+m)));
  1300. }
  1301. /**************
  1302. * Yxy
  1303. **************/
  1304. color toYXY(color c) {
  1305. PVector xyz = _toXYZ(getNR(c), getNG(c), getNB(c));
  1306. float sum = xyz.x + xyz.y + xyz.z;
  1307. float x = xyz.x > 0 ? xyz.x / sum : 0.0;
  1308. float y = xyz.y > 0 ? xyz.y / sum : 0.0;
  1309. return blendRGB(c,
  1310. (int)map(xyz.y, 0, RANGE_Y, 0, 255),
  1311. (int)map(x, 0.0, 1.0, 0, 255),
  1312. (int)map(y, 0.0, 1.0, 0, 255));
  1313. }
  1314. color fromYXY(color c) {
  1315. float Y = map(getR(c), 0, 255, 0, RANGE_Y);
  1316. float x = map(getG(c), 0, 255, 0, 1.0);
  1317. float y = map(getB(c), 0, 255, 0, 1.0);
  1318. float divy = Y / (y>0 ? y : 1.0e-6);
  1319. return _fromXYZ(c, x * divy, Y, (1-x-y)*divy);
  1320. }
  1321. /**************
  1322. * XYZ
  1323. **************/
  1324. // FIXME: range from 0 to 1
  1325. float correctionxyz(float n) {
  1326. return (n > 0.04045 ? pow((n + 0.055) / 1.055, 2.4) : n / 12.92) * 100.0;
  1327. }
  1328. PVector _toXYZ(float rr, float gg, float bb) {
  1329. float r = correctionxyz(rr);
  1330. float g = correctionxyz(gg);
  1331. float b = correctionxyz(bb);
  1332. return new PVector(r * 0.4124 + g * 0.3576 + b * 0.1805,
  1333. r * 0.2126 + g * 0.7152 + b * 0.0722,
  1334. r * 0.0193 + g * 0.1192 + b * 0.9505);
  1335. }
  1336. color toXYZ(color c) {
  1337. PVector xyz = _toXYZ(getNR(c), getNG(c), getNB(c));
  1338. return blendRGB(c,
  1339. (int)map(xyz.x, 0, RANGE_X, 0, 255),
  1340. (int)map(xyz.y, 0, RANGE_Y, 0, 255),
  1341. (int)map(xyz.z, 0, RANGE_Z, 0, 255));
  1342. }
  1343. float recorrectionxyz(float n) {
  1344. return n > 0.0031308 ? 1.055 * pow(n, corrratio) - 0.055 : 12.92 * n;
  1345. }
  1346. // FIXME: range from 0 to 1
  1347. color _fromXYZ(color c, float xx, float yy, float zz) {
  1348. float x = xx/100.0;
  1349. float y = yy/100.0;
  1350. float z = zz/100.0;
  1351. int r = round(255.0*recorrectionxyz(x * 3.2406 + y * -1.5372 + z * -0.4986));
  1352. int g = round(255.0*recorrectionxyz(x * -0.9689 + y * 1.8758 + z * 0.0415));
  1353. int b = round(255.0*recorrectionxyz(x * 0.0557 + y * -0.2040 + z * 1.0570));
  1354. return blendRGB(c, r, g, b);
  1355. }
  1356. color fromXYZ(color c) {
  1357. float x = map(getR(c), 0, 255, 0, RANGE_X);
  1358. float y = map(getG(c), 0, 255, 0, RANGE_Y);
  1359. float z = map(getB(c), 0, 255, 0, RANGE_Z);
  1360. return _fromXYZ(c, x, y, z);
  1361. }
  1362. /**************
  1363. * CMY
  1364. **************/
  1365. color toCMY(color c) {
  1366. return blendRGB(c, 255-getR(c), 255-getG(c), 255-getB(c));
  1367. }
  1368. color fromCMY(color c) {
  1369. return toCMY(c);
  1370. }
  1371. /**************
  1372. * OHTA
  1373. **************/
  1374. color fromOHTA(color c) {
  1375. int I1 = getR(c);
  1376. float I2 = map(getG(c), 0, 255, -127.5, 127.5);
  1377. float I3 = map(getB(c), 0, 255, -127.5, 127.5);
  1378. int R = (int)(I1+1.00000*I2-0.66668*I3);
  1379. int G = (int)(I1+1.33333*I3);
  1380. int B = (int)(I1-1.00000*I2-0.66668*I3);
  1381. return blendRGB(c, R, G, B);
  1382. }
  1383. color toOHTA(color c) {
  1384. int R = getR(c);
  1385. int G = getG(c);
  1386. int B = getB(c);
  1387. int I1 = (int)(0.33333*R+0.33334*G+0.33333*B);
  1388. int I2 = (int)map(0.5*(R-B), -127.5, 127.5, 0, 255);
  1389. int I3 = (int)map(-0.25000*R+0.50000*G-0.25000*B, -127.5, 127.5, 0, 255);
  1390. return blendRGB(c, I1, I2, I3);
  1391. }
  1392. ////
  1393. // 1/n table for n=0..255 - to speed up color conversions things
  1394. final static float[] r255 = {
  1395. 0.0, 0.003921569, 0.007843138, 0.011764706, 0.015686275, 0.019607844, 0.023529412, 0.02745098, 0.03137255, 0.03529412, 0.039215688,
  1396. 0.043137256, 0.047058824, 0.050980393, 0.05490196, 0.05882353, 0.0627451, 0.06666667, 0.07058824, 0.07450981, 0.078431375, 0.08235294,
  1397. 0.08627451, 0.09019608, 0.09411765, 0.09803922, 0.101960786, 0.105882354, 0.10980392, 0.11372549, 0.11764706, 0.12156863, 0.1254902,
  1398. 0.12941177, 0.13333334, 0.13725491, 0.14117648, 0.14509805, 0.14901961, 0.15294118, 0.15686275, 0.16078432, 0.16470589, 0.16862746,
  1399. 0.17254902, 0.1764706, 0.18039216, 0.18431373, 0.1882353, 0.19215687, 0.19607843, 0.2, 0.20392157, 0.20784314, 0.21176471, 0.21568628,
  1400. 0.21960784, 0.22352941, 0.22745098, 0.23137255, 0.23529412, 0.23921569, 0.24313726, 0.24705882, 0.2509804, 0.25490198, 0.25882354,
  1401. 0.2627451, 0.26666668, 0.27058825, 0.27450982, 0.2784314, 0.28235295, 0.28627452, 0.2901961, 0.29411766, 0.29803923, 0.3019608, 0.30588236,
  1402. 0.30980393, 0.3137255, 0.31764707, 0.32156864, 0.3254902, 0.32941177, 0.33333334, 0.3372549, 0.34117648, 0.34509805, 0.34901962, 0.3529412,
  1403. 0.35686275, 0.36078432, 0.3647059, 0.36862746, 0.37254903, 0.3764706, 0.38039216, 0.38431373, 0.3882353, 0.39215687, 0.39607844, 0.4,
  1404. 0.40392157, 0.40784314, 0.4117647, 0.41568628, 0.41960785, 0.42352942, 0.42745098, 0.43137255, 0.43529412, 0.4392157, 0.44313726,
  1405. 0.44705883, 0.4509804, 0.45490196, 0.45882353, 0.4627451, 0.46666667, 0.47058824, 0.4745098, 0.47843137, 0.48235294, 0.4862745, 0.49019608,
  1406. 0.49411765, 0.49803922, 0.5019608, 0.5058824, 0.50980395, 0.5137255, 0.5176471, 0.52156866, 0.5254902, 0.5294118, 0.53333336, 0.5372549,
  1407. 0.5411765, 0.54509807, 0.54901963, 0.5529412, 0.5568628, 0.56078434, 0.5647059, 0.5686275, 0.57254905, 0.5764706, 0.5803922, 0.58431375,
  1408. 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,
  1409. 0.6392157, 0.6431373, 0.64705884, 0.6509804, 0.654902, 0.65882355, 0.6627451, 0.6666667, 0.67058825, 0.6745098, 0.6784314, 0.68235296,
  1410. 0.6862745, 0.6901961, 0.69411767, 0.69803923, 0.7019608, 0.7058824, 0.70980394, 0.7137255, 0.7176471, 0.72156864, 0.7254902, 0.7294118,
  1411. 0.73333335, 0.7372549, 0.7411765, 0.74509805, 0.7490196, 0.7529412, 0.75686276, 0.7607843, 0.7647059, 0.76862746, 0.77254903, 0.7764706,
  1412. 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,
  1413. 0.83137256, 0.8352941, 0.8392157, 0.84313726, 0.84705883, 0.8509804, 0.85490197, 0.85882354, 0.8627451, 0.8666667, 0.87058824, 0.8745098,
  1414. 0.8784314, 0.88235295, 0.8862745, 0.8901961, 0.89411765, 0.8980392, 0.9019608, 0.90588236, 0.9098039, 0.9137255, 0.91764706, 0.92156863,
  1415. 0.9254902, 0.92941177, 0.93333334, 0.9372549, 0.9411765, 0.94509804, 0.9490196, 0.9529412, 0.95686275, 0.9607843, 0.9647059, 0.96862745,
  1416. 0.972549, 0.9764706, 0.98039216, 0.9843137, 0.9882353, 0.99215686, 0.99607843, 1.0
  1417. };