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.

effects.pde 45KB

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