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 66KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612
  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. /*
  798. SLITSCAN
  799. */
  800. class SLITSCAN extends Shader {
  801. int[] fx;
  802. int[] fy;
  803. float[] phx;
  804. float[] phy;
  805. int[] sx, sy;
  806. boolean[] skipfx;
  807. boolean[] skipfy;
  808. boolean dox, doy;
  809. PImage buffer;
  810. float[][] ft = new float[2][32];
  811. int depth; // number of octaves
  812. int fxnum;
  813. int fynum;
  814. SLITSCAN() {
  815. name = "fxSlitSscan";
  816. buffer = createImage(renderer.width, renderer.height, ARGB);
  817. for (int i=0; i<32; i++) {
  818. ft[0][i] = pow(2.0, i);
  819. ft[1][i] = 0.5*1.0/ft[0][i];
  820. }
  821. }
  822. void apply() {
  823. renderer.beginDraw();
  824. renderer.colorMode(RGB);
  825. renderer.noStroke();
  826. colorMode(RGB);
  827. renderer.fill(255);
  828. buffer.resize(renderer.width, renderer.height);
  829. buffer = renderer.get(0, 0, renderer.width, renderer.height);
  830. //int s = buffer.width>buffer.height?buffer.height:buffer.width;
  831. int s = min(buffer.width, buffer.height);
  832. depth = (int)(log(s)/log(2));
  833. fxnum = (int)random(depth); ////
  834. fynum = (int)random(depth); ////
  835. fx = new int[fxnum+1];
  836. fy = new int[fynum+1];
  837. sx = new int[fxnum+1];
  838. sy = new int[fynum+1];
  839. phx = new float[fxnum+1];
  840. phy = new float[fynum+1];
  841. skipfx = new boolean[fxnum+1];
  842. skipfy = new boolean[fynum+1];
  843. for (int i=0; i<fxnum; i++) {
  844. fx[i]=(int)random(6);
  845. phx[i] = random(1);
  846. skipfx[i] = random(1)<0.2;
  847. sx[i] = random(1)<0.2?-1:1;
  848. }
  849. for (int i=0; i<fynum; i++) {
  850. fy[i]=(int)random(6);
  851. phy[i] = random(1);
  852. skipfy[i] = random(1)<0.2;
  853. sy[i] = random(1)<0.2?-1:1;
  854. }
  855. dox = random(1)<0.8;
  856. doy = dox?random(1)<0.8:true;
  857. float v=0;
  858. for (int y=0; y<buffer.height; y++)
  859. for (int x=0; x<buffer.width; x++) {
  860. float iy = map(y, 0, buffer.height, 0, 1);
  861. v=0;
  862. if (doy) for (int i=0; i<fy.length; i++)
  863. if (!skipfy[i]) v+=sy[i]*getValue(fy[i], iy, i, phy[i]);
  864. float ry = 2*iy+v;
  865. float y2 = (3*buffer.height+ry * buffer.height/2)%buffer.height;
  866. float ix = map(x, 0, buffer.width, 0, 1);
  867. v=0;
  868. if (dox) for (int i=0; i<fx.length; i++)
  869. if (!skipfx[i]) v+=sx[i]*getValue(fx[i], ix, i, phx[i]);
  870. float rx = 2*ix+v;
  871. float x2 = (3*buffer.width+rx * buffer.width/2)%buffer.width;
  872. renderer.fill(buffer.get((int)x2, (int)y2));
  873. renderer.rect(x, y, 1, 1);
  874. }
  875. renderer.endDraw();
  876. }
  877. float getValue(int fun, float idx, int freq, float phase) {
  878. switch(fun) {
  879. case 0:
  880. return getSin(idx, freq, phase);
  881. case 1:
  882. return getSaw(idx, freq, phase);
  883. case 2:
  884. return getTriangle(idx, freq, phase);
  885. case 3:
  886. return getCutTriangle(idx, freq, phase);
  887. case 4:
  888. return getSquare(idx, freq, phase);
  889. case 5:
  890. return getNoise(idx, freq, phase);
  891. default:
  892. return getSin(idx, freq, phase);
  893. }
  894. }
  895. float getNoise(float idx, int freq, float phase) {
  896. return 2*ft[1][freq]*(noise((idx+phase)*ft[0][freq])-0.5);
  897. }
  898. float getSin(float idx, int freq, float phase) {
  899. float p = ft[0][freq];
  900. return ft[1][freq] * sin(idx*TWO_PI*p+phase*TWO_PI);
  901. }
  902. float getSaw(float idx, int freq, float phase) {
  903. float p = ft[0][freq];
  904. float rp = 2.0*ft[1][freq];
  905. float p2 = p*((idx+phase+ft[1][freq])%1.0);
  906. return rp*(p2-floor(p2)-0.5);
  907. }
  908. float getSquare(float idx, int freq, float phase) {
  909. float p = ft[0][freq];
  910. float rp = ft[1][freq];
  911. return (((idx*p)+phase)%1.0)<0.5?rp:-rp;
  912. }
  913. float getTriangle(float idx, int freq, float phase) {
  914. return 2*abs(getSaw(idx, freq, phase+0.5*ft[1][freq]))-ft[1][freq];
  915. }
  916. float getCutTriangle(float idx, int freq, float phase) {
  917. return constrain(getTriangle(idx, freq, phase), -ft[1][freq+1], ft[1][freq+1]);
  918. }
  919. }
  920. /*
  921. WAHWAH
  922. */
  923. class WAHWAH extends Shader {
  924. float sequence, lfoskip, xn1, xn2, yn1, yn2, b0, b1, b2, a0, a1, a2, freqofs, freq, freqoff, startsequence, res, depth;
  925. float mCurRate = 0.4, skipcount = 0;
  926. int lfoskipsamples = 0;
  927. float frequency, omega, sn, cs, alpha;
  928. float in, out;
  929. float val;
  930. WAHWAH() {
  931. name = "fxWahWah";
  932. //params.add(new Param("mode", INTVAL, 0, 1, new int[]{RANDOM, SQUAR}));
  933. params.add(new Param ("resolution", FLOATVAL, 1, 100, new int[]{TRIANG, SINE, RAMPUPDOWN, }));
  934. params.add(new Param ("depth", FLOATVAL, 0.0001, 1, new int[]{TRIANG, SINE, RAMPUPDOWN }));
  935. params.add(new Param ("frequency offset", FLOATVAL, 0, 1, new int[]{TRIANG, SINE, RAMPUPDOWN }));
  936. params.add(new Param ("mCurRate", FLOATVAL, 0, 1, new int[]{TRIANG, SINE, RAMPUPDOWN }));
  937. // params.add(new Param ("blend mode", INTVAL, 0, this.blends.length-1, new int[]{RANDOM }));
  938. }
  939. void apply() {
  940. res = params.get(0).value;
  941. depth = params.get(1).value;
  942. freqofs = params.get(2).value;
  943. //res = 12.5
  944. //depth = 0.8;
  945. //freqofs = 0.9;
  946. freq = 1.5;
  947. startsequence = 0.2;
  948. lfoskip = freq * 2 * PI / mCurRate;
  949. skipcount = xn1 = xn2 = yn1 = yn2 = b0 = b1 = b2 = a0 = a1 = a2 = 0;
  950. sequence = startsequence;
  951. renderer.beginDraw();
  952. renderer.colorMode(RGB);
  953. renderer.loadPixels();
  954. float[] rgb = new float[3];
  955. for ( int i = 0, len = renderer.pixels.length; i < len; i++) {
  956. rgb[0] = red(renderer.pixels[i]);
  957. rgb[1] = green(renderer.pixels[i]);
  958. rgb[2] = blue(renderer.pixels[i]);
  959. for ( int ri = 0; ri < 3; ri++ ) {
  960. in = map(rgb[ri], 0, 255, 0, 1);
  961. frequency = (1+cos(skipcount * lfoskip + sequence ))/2;
  962. frequency = frequency * depth * (1-freqofs) + freqofs;
  963. frequency = exp((frequency - 1) * 6 );
  964. omega = PI * frequency;
  965. sn = sin(omega);
  966. cs = cos(omega);
  967. alpha = sn/(2*res);
  968. b0 = (1-cs) /2;
  969. b1 = 1 - cs;
  970. b2 = (1-cs)/2;
  971. a0 = 1 + alpha;
  972. a1 = -2 * cs;
  973. a2 = 1 - alpha;
  974. out = ( b0 * in + b1 * xn1 + b2 * xn2 - a1 * yn1 - a2 * yn2 ) / a0;
  975. xn2 = xn1;
  976. xn1 = in;
  977. yn2 = yn1;
  978. yn1 = out;
  979. rgb[ri] = map(out, 0, 1, 0, 255);
  980. }
  981. renderer.pixels[i] = color(rgb[0], rgb[1], rgb[2]);
  982. }
  983. renderer.updatePixels();
  984. renderer.endDraw();
  985. }
  986. }
  987. /*
  988. PHASER
  989. */
  990. class PHASER extends Shader {
  991. //float samplerate = 92230.0; // try setting this to 44100.0 or 2048.5 for kicks
  992. float samplerate = 44100; // try setting this to 44100.0 or 2048.5 for kicks
  993. int mode;
  994. PHASER() {
  995. name ="fxPhaser";
  996. params.add(new Param("mode", INTVAL, 0, 1, new int[]{RANDOM, SQUAR}));
  997. params.add(new Param ("frequency", FLOATVAL, 0.1, 40.0, new int[]{TRIANG, SINE, RAMPUPDOWN }));
  998. params.add(new Param ("depth", INTVAL, 1, 255, new int[]{TRIANG, SINE, RAMPUPDOWN }));
  999. params.add(new Param ("feedback", INTVAL, -100, 100, new int[]{TRIANG, SINE, RAMPUPDOWN }));
  1000. params.add(new Param ("phase", FLOATVAL, 0, TWO_PI, new int[]{TRIANG, SINE, RAMPUPDOWN }));
  1001. params.add(new Param ("stages", INTVAL, 1, 24, new int[]{TRIANG, SINE, RAMPUPDOWN }));
  1002. params.add(new Param ("sample rate", FLOATVAL, 512, 92230, new int[]{TRIANG, SINE, RAMPUPDOWN }));
  1003. //params.add(new Param ("frequency offset", FLOATVAL, 0, 1, new int[]{TRIANG, SINE, RAMPUPDOWN }));
  1004. }
  1005. /*
  1006. mDepth = (int) map(knobZero, 0, 255, 255, 0);
  1007. mFeedback = (int) map(knobOne, 0, 255, -100, 100);
  1008. // enable these for some more fun :)
  1009. if (mode == 1) {
  1010. mSampleRate = map(knobTwo, 0, 255, 1, 512);
  1011. mStages = (int) ( 2*map(knobThree, 0, 255, 1, 12));
  1012. }
  1013. */
  1014. void apply() {
  1015. mode = (int)params.get(0).value;
  1016. float mFreq = params.get(1).value;
  1017. int mDryWet = 255;
  1018. int mDepth = (int)params.get(2).value;
  1019. int mFeedback = (int)params.get(3).value;
  1020. float mPhase = params.get(4).value;
  1021. //these two are only changed if mode = 1
  1022. int mStages = 2;
  1023. float mSampleRate = samplerate;
  1024. renderer.beginDraw();
  1025. renderer.colorMode(RGB);
  1026. renderer.loadPixels();
  1027. //constants
  1028. float phaserlfoshape = 4.0;
  1029. int lfoskipsamples = 20; //how many samples are processed before recomputing lfo
  1030. int numStages = 24;
  1031. //getParams
  1032. /*
  1033. Phaser Parameters
  1034. mFreq - Phaser's LFO frequency
  1035. mPhase - Phaser's LFO startsequence (radians), needed for stereo Phasers
  1036. mDepth - Phaser depth (0 - no depth, 255 - max depth)
  1037. mStages - Phaser stages (recomanded from 2 to 16-24, and EVEN NUMBER)
  1038. mDryWet - Dry/wet mix, (0 - dry, 128 - dry=wet, 255 - wet)
  1039. mFeedback - Phaser FeedBack (0 - no feedback, 100 = 100% Feedback,
  1040. -100 = -100% FeedBack)
  1041. */
  1042. // enable these for some more fun :)
  1043. if (mode == 1) {
  1044. mStages = (int)params.get(5).value;
  1045. mSampleRate = params.get(6).value;
  1046. }
  1047. //init
  1048. float gain = 0, fbout = 0;
  1049. float lfoskip = mFreq * 2 * PI / mSampleRate;
  1050. float sequence = mPhase * PI / 180;
  1051. float[] old = new float[mStages];
  1052. for ( int j = 0; j < mStages; j++) {
  1053. old[j] = 0.0;
  1054. }
  1055. /* EffectPhaser::ProcessBlock */
  1056. int skipcount = 0;
  1057. float[] rgb = new float[3];
  1058. for ( int i = 0, l = renderer.pixels.length; i<l; i++ ) {
  1059. color c = renderer.pixels[i];
  1060. rgb[0] = map(red(c), 0, 255, 0, 1);
  1061. rgb[1] = map(green(c), 0, 255, 0, 1);
  1062. rgb[2] = map(blue(c), 0, 255, 0, 1);
  1063. for ( int ci = 0; ci < 3; ci++) {
  1064. float in = rgb[ci];
  1065. float m = in + fbout * mFeedback / 100;
  1066. if ( (( skipcount++) % lfoskipsamples ) == 0 ) { //recomopute lfo
  1067. gain = (1.0 + cos(skipcount * lfoskip + sequence)) / 2.0; //compute sine between 0 and 1
  1068. gain = exp(gain * phaserlfoshape) / exp(phaserlfoshape); // change lfo shape
  1069. gain = 1.0 - gain / 255.0 * mDepth; // attenuate the lfo
  1070. }
  1071. //phasing routine
  1072. for ( int j = 0; j<mStages; j++) {
  1073. float tmp = old[j];
  1074. old[j] = gain * tmp + m;
  1075. m = tmp - gain * old[j];
  1076. }
  1077. fbout = m;
  1078. rgb[ci] = (float) (( m * mDryWet + in * (255-mDryWet)) / 255);
  1079. }
  1080. color rc = color(
  1081. map(rgb[0], 0, 1, 0, 255),
  1082. map(rgb[1], 0, 1, 0, 255),
  1083. map(rgb[2], 0, 1, 0, 255));
  1084. renderer.pixels[i] = rc;
  1085. }
  1086. renderer.updatePixels();
  1087. renderer.endDraw();
  1088. }
  1089. }
  1090. /*
  1091. ECHO
  1092. */
  1093. class ECHO extends Shader {
  1094. int mode = 0;
  1095. PImage result;
  1096. ECHO() {
  1097. name = "fxEcho";
  1098. params.add(new Param("mode", INTVAL, 0, 1, new int[]{RANDOM, SQUAR}));
  1099. params.add(new Param ("xp", INTVAL, 0, 100, new int[]{TRIANG, SINE, RAMPUPDOWN }));
  1100. params.add(new Param ("yp", INTVAL, 0, 100, new int[]{TRIANG, SINE, RAMPUPDOWN }));
  1101. result = createImage(renderer.width, renderer.height, RGB);
  1102. }
  1103. void apply() {
  1104. mode = (int)params.get(0).value;
  1105. int xp = (int)params.get(1).value;
  1106. int yp = (int)params.get(2).value;
  1107. renderer.beginDraw();
  1108. renderer.colorMode(RGB);
  1109. colorMode(RGB);
  1110. if (mode == 0) {
  1111. renderer.image(auEcho(renderer, xp, yp), 0, 0);
  1112. } else if (mode == 1) {
  1113. renderer.image(auEchoWTF(renderer, xp, yp), 0, 0);
  1114. }
  1115. renderer.endDraw();
  1116. }
  1117. PImage auEcho(PImage img, int xp, int yp) {
  1118. result.resize(img.width, img.height);
  1119. float _delay = map(xp, 0, 100, 0.001, 1.0);
  1120. float decay = map(yp, 0, 100, 0.0, 1.0);
  1121. int delay = (int)(img.pixels.length * _delay);
  1122. color[] history = new color[img.pixels.length];
  1123. int blendMode = BLEND;
  1124. img.loadPixels();
  1125. result.loadPixels();
  1126. for ( int i = 0, l = img.pixels.length; i<l; i++) {
  1127. history[i] = img.pixels[i];
  1128. }
  1129. for ( int i = 0, l = img.pixels.length; i<l; i++) {
  1130. int fromPos = i-delay < 0 ? l-abs(i-delay) : i-delay;
  1131. color fromColor = history[fromPos];
  1132. float r = red(fromColor) * decay;
  1133. float g = green(fromColor) * decay;
  1134. float b = blue(fromColor) * decay;
  1135. color origColor = history[i];
  1136. color toColor = color(
  1137. r + red(origColor),
  1138. g + green(origColor),
  1139. b + blue(origColor) );
  1140. result.pixels[i] = history[i] = blendColor(origColor, toColor, blendMode);
  1141. }
  1142. return result;
  1143. }
  1144. PImage auEchoWTF(PImage img, int xp, int yp) {
  1145. result.resize(img.width, img.height);
  1146. float delay = map(xp, 0, 100, 0.001, 5);
  1147. float decay = map(yp, 0, 100, 0.0, 5.0);
  1148. int histPos = 0;
  1149. int histLen = img.pixels.length*3;
  1150. float[] history = new float[histLen*3];
  1151. img.loadPixels();
  1152. result.loadPixels();
  1153. float ibuf = 0.0, obuf = 0.0;
  1154. float[] rgb = new float[3];
  1155. for ( int i = 0, l = img.pixels.length; i<l; i++, histPos++) {
  1156. color c = img.pixels[i];
  1157. rgb[0] = map(red(c), 0, 255, 0, 1);
  1158. rgb[1] = map(green(c), 0, 255, 0, 1);
  1159. rgb[2] = map(blue(c), 0, 255, 0, 1);
  1160. history[i] = rgb[0];
  1161. history[i+1] = rgb[1];
  1162. history[i+2] = rgb[2];
  1163. }
  1164. for ( int i = 0, l = img.pixels.length; i<l; i++, histPos++) {
  1165. color c = img.pixels[i];
  1166. rgb[0] = map(red(c), 0, 255, 0, 1);
  1167. rgb[1] = map(green(c), 0, 255, 0, 1);
  1168. rgb[2] = map(blue(c), 0, 255, 0, 1);
  1169. if ( histPos == histLen ) histPos = 0;
  1170. for ( int ri = 0; ri < 3; ri++ ) {
  1171. history[histPos+ri] = rgb[ri] = rgb[ri] + history[histPos+ri] * decay;
  1172. }
  1173. color out = color(
  1174. (int)map(rgb[0], 0, 1, 0, 255),
  1175. (int)map(rgb[1], 0, 1, 0, 255),
  1176. (int)map(rgb[2], 0, 1, 0, 255));
  1177. result.pixels[i] = out;
  1178. }
  1179. return result;
  1180. }
  1181. }
  1182. /*
  1183. DARKER
  1184. */
  1185. class DARKER extends Shader {
  1186. float thresh = 127;
  1187. float darken = 150;
  1188. int mode = 1;
  1189. int bangCount = 0;
  1190. DARKER() {
  1191. name = "fxDarker";
  1192. params.add(new Param("count", INTVAL, 1, 4, new int[]{TRIANG, SINE, RAMPUPDOWN, TAN, TANINVERSE}));
  1193. params.add(new Param("threshold", INTVAL, 60, 180, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  1194. params.add(new Param("darken", INTVAL, 140, 220, new int[]{TRIANG, SINE, RAMPUPDOWN }));
  1195. params.add(new Param("mode", INTVAL, 0, 1, new int[]{SQUAR, RANDOM}));
  1196. //thresh = int(random(60, 180));
  1197. //darken = int(random(140, 220));
  1198. }
  1199. void apply() {
  1200. // bright = knobZero;
  1201. int count = int(params.get(0).value);
  1202. darken = int(params.get(1).value);
  1203. thresh = int(params.get(2).value);
  1204. mode = int(params.get(3).value);
  1205. renderer.beginDraw();
  1206. renderer.colorMode(HSB);
  1207. colorMode(HSB);
  1208. renderer.loadPixels();
  1209. if (mode == 0) {
  1210. for (int h = 1; h < count+1; h++) {
  1211. for (int i = 0; i < renderer.width*renderer.height; i++) {
  1212. float hue = hue(renderer.pixels[i]);
  1213. float sat = saturation(renderer.pixels[i]);
  1214. float bright = brightness(renderer.pixels[i]);
  1215. if (bright > thresh/h) {
  1216. bright -= darken/h;
  1217. constrain(bright, 0, 255);
  1218. }
  1219. color c = color(hue, sat, bright);
  1220. renderer.pixels[i] = c;
  1221. }
  1222. }
  1223. } else if (mode == 1) {
  1224. for (int h = 1; h < count+1; h++) {
  1225. for (int i = 0; i < renderer.width*renderer.height; i++) {
  1226. float hue = hue(renderer.pixels[i]);
  1227. float sat = saturation(renderer.pixels[i]);
  1228. float bright = brightness(renderer.pixels[i]);
  1229. if (bright < thresh/h) {
  1230. bright -= darken/h;
  1231. constrain(bright, 0, 255);
  1232. }
  1233. color c = color(hue, sat, bright);
  1234. renderer.pixels[i] = c;
  1235. }
  1236. }
  1237. }
  1238. renderer.updatePixels();
  1239. renderer.endDraw();
  1240. colorMode(RGB);
  1241. }
  1242. }
  1243. /*
  1244. BRIGHTER
  1245. */
  1246. class BRIGHTER extends Shader {
  1247. float thresh = 120;
  1248. float thresh2 = 150;
  1249. float brighten = 180;
  1250. float speed;
  1251. BRIGHTER() {
  1252. name = "fxBrighter";
  1253. params.add(new Param("threshold", INTVAL, 0, 255, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  1254. params.add(new Param("threshold 2", INTVAL, 0, 255, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  1255. params.add(new Param("brighten", INTVAL, 0, 255, new int[]{TRIANG, SINE, RAMPUPDOWN }));
  1256. }
  1257. void apply() {
  1258. brighten = int(params.get(2).value);
  1259. thresh2 = int(params.get(1).value);
  1260. thresh = int(params.get(0).value);
  1261. renderer.beginDraw();
  1262. renderer.colorMode(HSB);
  1263. colorMode(HSB);
  1264. renderer.loadPixels();
  1265. for (int i = 0; i < renderer.width*renderer.height; i++) {
  1266. float hue = hue(renderer.pixels[i]);
  1267. float sat = saturation(renderer.pixels[i]);
  1268. float bright = brightness(renderer.pixels[i]);
  1269. if (bright < thresh && bright > thresh2) {
  1270. bright += brighten;
  1271. constrain(bright, 0, 255);
  1272. }
  1273. color c = color(hue, sat, bright);
  1274. renderer.pixels[i] = c;
  1275. }
  1276. renderer.colorMode(RGB);
  1277. renderer.updatePixels();
  1278. renderer.endDraw();
  1279. colorMode(RGB);
  1280. }
  1281. }
  1282. /*
  1283. AMPLIFY
  1284. */
  1285. //13
  1286. class AMPLIFY extends Shader {
  1287. int spawnT;
  1288. float h, s, b;
  1289. AMPLIFY() {
  1290. name = "fxAmplify";
  1291. params.add(new Param("hue / red", FLOATVAL, -255, 255, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  1292. params.add(new Param("saturation / green", FLOATVAL, -255, 255, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  1293. params.add(new Param("brightness / blue", FLOATVAL, -255, 255, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  1294. params.add(new Param("RGB / HSB", INTVAL, 0, 1, new int[]{SQUAR, RANDOM}));
  1295. }
  1296. void apply() {
  1297. renderer.beginDraw();
  1298. if (params.get(3).value > 0) colorMode(HSB);
  1299. else {
  1300. colorMode(RGB);
  1301. }
  1302. renderer.loadPixels();
  1303. for (int i = 0; i < renderer.width*renderer.height; i++) {
  1304. float h = hue(renderer.pixels[i]);
  1305. float s = saturation(renderer.pixels[i]);
  1306. float b = brightness(renderer.pixels[i]);
  1307. renderer.pixels[i] = color(h+params.get(0).value, s+params.get(1).value, b+params.get(2).value);
  1308. }
  1309. renderer.updatePixels();
  1310. renderer.endDraw();
  1311. colorMode(RGB);
  1312. }
  1313. }
  1314. /*
  1315. BROKENCOLORROT
  1316. */
  1317. class BROKENCOLORROT extends Shader {
  1318. int spawnT;
  1319. float h, s, b;
  1320. BROKENCOLORROT() {
  1321. name = "fxBrokenColorRot";
  1322. params.add(new Param("hue", INTVAL, 0, 255, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  1323. params.add(new Param("saturation", INTVAL, 0, 255, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  1324. params.add(new Param("brightness", INTVAL, 0, 255, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  1325. params.add(new Param("mode", INTVAL, 0, 1, new int[]{SQUAR, RANDOM}));
  1326. }
  1327. void apply() {
  1328. renderer.beginDraw();
  1329. if (params.get(3).value > 0) colorMode(HSB);
  1330. else {
  1331. colorMode(RGB);
  1332. }
  1333. renderer.loadPixels();
  1334. float h = params.get(0).value;
  1335. float s = params.get(1).value;
  1336. float b = params.get(2).value;
  1337. for (int i = 0; i < renderer.width*renderer.height; i++) {
  1338. h = hue(renderer.pixels[i])+h;
  1339. if (h > 255) h -= 255;
  1340. s = saturation(renderer.pixels[i])+s;
  1341. if (s > 255) s -= 255;
  1342. b = brightness(renderer.pixels[i])+b;
  1343. if (b > 255) b -= 255;
  1344. renderer.pixels[i] = color(h, s, b);
  1345. }
  1346. renderer.updatePixels();
  1347. renderer.endDraw();
  1348. colorMode(RGB);
  1349. }
  1350. }
  1351. /*
  1352. POSTERIZE
  1353. */
  1354. class POSTER extends Shader {
  1355. POSTER() {
  1356. name = "fxPosterize";
  1357. params.add(new Param("levels", INTVAL, 2, 10, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  1358. }
  1359. void apply() {
  1360. renderer.beginDraw();
  1361. renderer.filter(POSTERIZE, (int)params.get(0).value);
  1362. renderer.endDraw();
  1363. }
  1364. }
  1365. /*
  1366. DUAL
  1367. */
  1368. class DUAL extends Shader {
  1369. PImage buffer;
  1370. int dualColor;
  1371. int dirx = 1;
  1372. int diry = 1;
  1373. DUAL() {
  1374. name = "fxDual";
  1375. params.add(new Param("dual color", INTVAL, 2000000, 15000000, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  1376. params.add(new Param("flip direction", INTVAL, 0, 3, new int[]{RANDOM}));
  1377. params.add(new Param("mode", INTVAL, 0, 1, new int[]{SQUAR, RANDOM}));
  1378. buffer = createImage(renderer.width, renderer.height, ARGB);
  1379. }
  1380. void apply() {
  1381. switch((int)params.get(1).value) {
  1382. case(0):
  1383. dirx = 1;
  1384. diry = 1;
  1385. break;
  1386. case(1):
  1387. dirx = -1;
  1388. diry = 1;
  1389. break;
  1390. case(2):
  1391. dirx = 1;
  1392. diry = -1;
  1393. break;
  1394. case(3):
  1395. dirx = -1;
  1396. diry = -1;
  1397. break;
  1398. }
  1399. dualColor = (int)params.get(0).value;
  1400. buffer.resize(renderer.width, renderer.height);
  1401. renderer.beginDraw();
  1402. renderer.loadPixels();
  1403. buffer.loadPixels();
  1404. if ((int)params.get(2).value > 0) {
  1405. for (int i = 0; i < renderer.width*renderer.height; i++) {
  1406. buffer.pixels[i] = renderer.pixels[i]+dualColor;
  1407. }
  1408. } else {
  1409. for (int i = 0; i < renderer.width*renderer.height; i++) {
  1410. buffer.pixels[i] = renderer.pixels[i]+dualColor;
  1411. buffer.pixels[i] = buffer.pixels[i]+i/10;
  1412. }
  1413. }
  1414. buffer.updatePixels();
  1415. renderer.updatePixels();
  1416. renderer.pushMatrix();
  1417. renderer.scale(dirx, diry);
  1418. renderer.image(buffer, 0, 0, dirx * renderer.width, diry * renderer.height);
  1419. renderer.popMatrix();
  1420. renderer.endDraw();
  1421. }
  1422. }
  1423. /*
  1424. GRAUZONE
  1425. */
  1426. class GRAUZONE extends Shader {
  1427. int nFrames = 20;
  1428. int iWrite = 0, iRead = 1;
  1429. PImage[] buffer;
  1430. PGraphics grauz;
  1431. GRAUZONE() {
  1432. name = "fxGrauzone";
  1433. params.add(new Param("delay (in frames)", INTVAL, 3, 100, new int[]{RANDOM}));
  1434. nFrames = (int)params.get(0).value;
  1435. buffer = new PImage[nFrames];
  1436. }
  1437. int nFramesPrev;
  1438. void apply() {
  1439. nFramesPrev = nFrames;
  1440. if (nFramesPrev != (int)params.get(0).value) {
  1441. iWrite = 0;
  1442. iRead = 1;
  1443. int nFramesOld = nFrames;
  1444. nFrames = (int)params.get(0).value;
  1445. if (nFrames > nFramesOld) {
  1446. buffer = new PImage[nFrames];
  1447. }
  1448. }
  1449. buffer[iWrite] = renderer.get();
  1450. grauz = createGraphics(renderer.width, renderer.height);
  1451. grauz.beginDraw();
  1452. // grauz.resize(renderer.width, renderer.height);
  1453. buffer[iWrite] = renderer.get();
  1454. if (buffer[iRead] != null) {
  1455. grauz.tint(255, 127);
  1456. buffer[iRead].filter(INVERT);
  1457. grauz.image(buffer[iRead], 0, 0, renderer.width, renderer.height);
  1458. grauz.tint(255, 255);
  1459. }
  1460. grauz.endDraw();
  1461. renderer.beginDraw();
  1462. renderer.image(grauz, 0, 0, renderer.width, renderer.height);
  1463. renderer.endDraw();
  1464. iWrite++;
  1465. iRead++;
  1466. if (iRead >= nFrames-1) {
  1467. iRead = 0;
  1468. }
  1469. if (iWrite >= nFrames-1) {
  1470. iWrite = 0;
  1471. }
  1472. }
  1473. }
  1474. /*
  1475. BUFFER
  1476. */
  1477. class BUFFER extends Shader {
  1478. BUFFER() {
  1479. name = "BUFFER";
  1480. }
  1481. }
  1482. // name
  1483. String getColorspaceName(int cs) {
  1484. switch(cs) {
  1485. case OHTA:
  1486. return "OHTA";
  1487. case CMY:
  1488. return "CMY";
  1489. case XYZ:
  1490. return "XYZ";
  1491. case YXY:
  1492. return "YXY";
  1493. case HCL:
  1494. return "HCL";
  1495. case LUV:
  1496. return "LUV";
  1497. case LAB:
  1498. return "LAB";
  1499. case HWB:
  1500. return "HWB";
  1501. case HSB:
  1502. return "HSB";
  1503. case RGGBG:
  1504. return "R-GGB-G";
  1505. case YPbPr:
  1506. return "YPbPr";
  1507. case YCbCr:
  1508. return "YCbCr";
  1509. case YDbDr:
  1510. return "YDbDr";
  1511. case GS:
  1512. return "Greyscale";
  1513. case YUV:
  1514. return "YUV";
  1515. default:
  1516. return "RGB";
  1517. }
  1518. }
  1519. // colorspace converters
  1520. color fromColorspace(color c, int cs) {
  1521. switch(cs) {
  1522. case OHTA:
  1523. return fromOHTA(c);
  1524. case CMY:
  1525. return fromCMY(c);
  1526. case XYZ:
  1527. return fromXYZ(c);
  1528. case YXY:
  1529. return fromYXY(c);
  1530. case HCL:
  1531. return fromHCL(c);
  1532. case LUV:
  1533. return fromLUV(c);
  1534. case LAB:
  1535. return fromLAB(c);
  1536. case HWB:
  1537. return fromHWB(c);
  1538. case HSB:
  1539. return fromHSB(c);
  1540. case RGGBG:
  1541. return fromRGGBG(c);
  1542. case YPbPr:
  1543. return fromYPbPr(c);
  1544. case YCbCr:
  1545. return fromYCbCr(c);
  1546. case YDbDr:
  1547. return fromYDbDr(c);
  1548. case GS:
  1549. return tofromGS(c);
  1550. case YUV:
  1551. return fromYUV(c);
  1552. default:
  1553. return c;
  1554. }
  1555. }
  1556. color toColorspace(color c, int cs) {
  1557. switch(cs) {
  1558. case OHTA:
  1559. return toOHTA(c);
  1560. case CMY:
  1561. return toCMY(c);
  1562. case XYZ:
  1563. return toXYZ(c);
  1564. case YXY:
  1565. return toYXY(c);
  1566. case HCL:
  1567. return toHCL(c);
  1568. case LUV:
  1569. return toLUV(c);
  1570. case LAB:
  1571. return toLAB(c);
  1572. case HWB:
  1573. return toHWB(c);
  1574. case HSB:
  1575. return toHSB(c);
  1576. case RGGBG:
  1577. return toRGGBG(c);
  1578. case YPbPr:
  1579. return toYPbPr(c);
  1580. case YCbCr:
  1581. return toYCbCr(c);
  1582. case YDbDr:
  1583. return toYDbDr(c);
  1584. case YUV:
  1585. return toYUV(c);
  1586. case GS:
  1587. return tofromGS(c);
  1588. default:
  1589. return c;
  1590. }
  1591. }
  1592. // Colorspace converters
  1593. final int getR(color c) {
  1594. return (c & 0xff0000) >> 16;
  1595. }
  1596. final int getG(color c) {
  1597. return (c & 0xff00) >> 8;
  1598. }
  1599. final int getB(color c) {
  1600. return c & 0xff;
  1601. }
  1602. final int getLuma(color c) {
  1603. return constrain((int)(0.2126*getR(c)+0.7152*getG(c)+0.0722*getB(c)), 0, 255);
  1604. }
  1605. int getChannel(color c, int ch) {
  1606. switch(ch) {
  1607. case 0 :
  1608. return getR(c);
  1609. case 1 :
  1610. return getG(c);
  1611. case 2 :
  1612. return getB(c);
  1613. default:
  1614. return 0;
  1615. }
  1616. }
  1617. // normalized versions
  1618. final float getNR(color c) {
  1619. return r255[(c & 0xff0000) >> 16];
  1620. }
  1621. final float getNG(color c) {
  1622. return r255[(c & 0xff00) >> 8];
  1623. }
  1624. final float getNB(color c) {
  1625. return r255[c & 0xff];
  1626. }
  1627. final float getNLuma(color c) {
  1628. return r255[getLuma(c)];
  1629. }
  1630. color blendRGB(color c, int r, int g, int b) {
  1631. return (c & 0xff000000) | (constrain(r, 0, 255) << 16) | (constrain(g, 0, 255) << 8 ) | constrain(b, 0, 255);
  1632. }
  1633. color blendRGB(color c, float r, float g, float b) {
  1634. return blendRGB(c, (int)(r*255), (int)(g*255), (int)(b*255));
  1635. }
  1636. /**************
  1637. * Greyscale
  1638. **************/
  1639. color tofromGS(color c) {
  1640. int l = getLuma(c);
  1641. return blendRGB(c, l, l, l);
  1642. }
  1643. /**************
  1644. * YUV
  1645. **************/
  1646. final static float Umax = 0.436 * 255.0;
  1647. final static float Vmax = 0.615 * 255.0;
  1648. color toYUV(color c) {
  1649. int R = getR(c);
  1650. int G = getG(c);
  1651. int B = getB(c);
  1652. int Y = (int)( 0.299*R+0.587*G+0.114*B);
  1653. int U = (int)map(-0.14713*R-0.28886*G+0.436*B, -Umax, Umax, 0, 255);
  1654. int V = (int)map(0.615*R-0.51499*G-0.10001*B, -Vmax, Vmax, 0, 255);
  1655. return blendRGB(c, Y, U, V);
  1656. }
  1657. color fromYUV(color c) {
  1658. int Y = getR(c);
  1659. float U = map(getG(c), 0, 255, -Umax, Umax);
  1660. float V = map(getB(c), 0, 255, -Vmax, Vmax);
  1661. int R = (int)(Y + 1.13983*V);
  1662. int G = (int)(Y - 0.39465*U - 0.58060*V);
  1663. int B = (int)(Y + 2.03211*U);
  1664. return blendRGB(c, R, G, B);
  1665. }
  1666. /**************
  1667. * YDbDr
  1668. **************/
  1669. color toYDbDr(color c) {
  1670. int R = getR(c);
  1671. int G = getG(c);
  1672. int B = getB(c);
  1673. int Y = (int)( 0.299*R+0.587*G+0.114*B);
  1674. int Db = (int)(127.5+(-0.450*R-0.883*G+1.333*B)/2.666);
  1675. int Dr = (int)(127.5+(-1.333*R+1.116*G+0.217*B)/2.666);
  1676. return blendRGB(c, Y, Db, Dr);
  1677. }
  1678. color fromYDbDr(color c) {
  1679. int Y = getR(c);
  1680. float Db = (getG(c)-127.5)*2.666;
  1681. float Dr = (getB(c)-127.5)*2.666;
  1682. int R = (int)(Y + 9.2303716147657e-05*Db-0.52591263066186533*Dr);
  1683. int G = (int)(Y - 0.12913289889050927*Db+0.26789932820759876*Dr);
  1684. int B = (int)(Y + 0.66467905997895482*Db-7.9202543533108e-05*Dr);
  1685. return blendRGB(c, R, G, B);
  1686. }
  1687. /**************
  1688. * YCbCr
  1689. **************/
  1690. color toYCbCr(color c) {
  1691. int R = getR(c);
  1692. int G = getG(c);
  1693. int B = getB(c);
  1694. int Y = (int)( 0.2988390*R+0.5868110*G+0.1143500*B);
  1695. int Cb = (int)(-0.168736*R-0.3312640*G+0.5000000*B+127.5);
  1696. int Cr = (int)( 0.5000000*R-0.4186880*G-0.0813120*B+127.5);
  1697. return blendRGB(c, Y, Cb, Cr);
  1698. }
  1699. color fromYCbCr(color c) {
  1700. int Y = getR(c);
  1701. float Cb = getG(c) - 127.5;
  1702. float Cr = getB(c) - 127.5;
  1703. int R = (int)(Y + 1.402*Cr)+1; // some fix
  1704. int G = (int)(Y-0.344136*Cb-0.714136*Cr);
  1705. int B = (int)(Y+1.772000*Cb)+1; // some fix
  1706. return blendRGB(c, R, G, B);
  1707. }
  1708. /**************
  1709. * YPbPr
  1710. **************/
  1711. color toYPbPr(color c) {
  1712. int R = getR(c);
  1713. int B = getB(c);
  1714. int Y = getLuma(c);
  1715. int Pb = B - Y;
  1716. int Pr = R - Y;
  1717. if (Pb<0) Pb+=256;
  1718. if (Pr<0) Pr+=256;
  1719. return blendRGB(c, Y, Pb, Pr);
  1720. }
  1721. color fromYPbPr(color c) {
  1722. int Y = getR(c);
  1723. int B = getG(c) + Y;
  1724. int R = getB(c) + Y;
  1725. if (R>255) R-=256;
  1726. if (B>255) B-=256;
  1727. int G = (int)((Y-0.2126*R-0.0722*B)/0.7152);
  1728. return blendRGB(c, R, G, B);
  1729. }
  1730. /**************
  1731. * R-G,G,B-G
  1732. **************/
  1733. color toRGGBG(color c) {
  1734. int G = getG(c);
  1735. int R = getR(c)-G;
  1736. int B = getB(c)-G;
  1737. if (R<0) R+=256;
  1738. if (B<0) B+=256;
  1739. return blendRGB(c, R, G, B);
  1740. }
  1741. color fromRGGBG(color c) {
  1742. int G = getG(c);
  1743. int R = getR(c)+G;
  1744. int B = getB(c)+G;
  1745. if (R>255) R-=256;
  1746. if (B>255) B-=256;
  1747. return blendRGB(c, R, G, B);
  1748. }
  1749. /**************
  1750. * HWB
  1751. **************/
  1752. color toHSB(color c) {
  1753. int R = getR(c);
  1754. int G = getG(c);
  1755. int B = getB(c);
  1756. int _min = min(R, G, B);
  1757. int _max = max(R, G, B);
  1758. float delta = _max-_min;
  1759. float saturation = delta/_max;
  1760. float brightness = r255[_max];
  1761. if (delta == 0.0) return blendRGB(c, 0.0, saturation, brightness);
  1762. float hue = 0;
  1763. if (R == _max) hue = (G-B)/delta;
  1764. else if (G == _max) hue = 2.0 + (B-R)/delta;
  1765. else hue = 4.0 + (R-G)/delta;
  1766. hue /= 6.0;
  1767. if (hue < 0.0) hue += 1.0;
  1768. return blendRGB(c, hue, saturation, brightness);
  1769. }
  1770. color fromHSB(color c) {
  1771. float S = getNG(c);
  1772. float B = getNB(c);
  1773. if (S == 0.0) return blendRGB(c, B, B, B);
  1774. float h = 6.0 * getNR(c);
  1775. float f = h-floor(h);
  1776. float p = B*(1.0-S);
  1777. float q = B*(1.0-S*f);
  1778. float t = B*(1.0-(S*(1.0-f)));
  1779. float r, g, b;
  1780. switch((int)h) {
  1781. case 1:
  1782. r=q;
  1783. g=B;
  1784. b=p;
  1785. break;
  1786. case 2:
  1787. r=p;
  1788. g=B;
  1789. b=t;
  1790. break;
  1791. case 3:
  1792. r=p;
  1793. g=q;
  1794. b=B;
  1795. break;
  1796. case 4:
  1797. r=t;
  1798. g=p;
  1799. b=B;
  1800. break;
  1801. case 5:
  1802. r=B;
  1803. g=p;
  1804. b=q;
  1805. break;
  1806. default:
  1807. r=B;
  1808. g=t;
  1809. b=p;
  1810. break;
  1811. }
  1812. return blendRGB(c, r, g, b);
  1813. }
  1814. /**************
  1815. * HWB
  1816. **************/
  1817. color toHWB(color c) {
  1818. int R = getR(c);
  1819. int G = getG(c);
  1820. int B = getB(c);
  1821. int w = min(R, G, B);
  1822. int v = max(R, G, B);
  1823. int hue;
  1824. if (v == w) hue = 255;
  1825. else {
  1826. float f = ((R == w) ? G-B : ((G == w) ? B-R : R-G));
  1827. float p = (R == w) ? 3.0 : ((G == w) ? 5.0 : 1.0);
  1828. hue = (int)map((p-f/(v-w))/6.0, 0, 1, 0, 254);
  1829. }
  1830. return blendRGB(c, hue, w, 255-v);
  1831. }
  1832. color fromHWB(color c) {
  1833. int H = getR(c);
  1834. int B = 255-getB(c);
  1835. if (H == 255) return blendRGB(c, B, B, B);
  1836. else {
  1837. float hue = map(H, 0, 254, 0, 6);
  1838. float v = r255[B];
  1839. float whiteness = getNG(c);
  1840. int i = (int)floor(hue);
  1841. float f = hue-i;
  1842. if ((i&0x01)!= 0) f=1.0-f;
  1843. float n = whiteness+f*(v-whiteness);
  1844. float r, g, b;
  1845. switch(i) {
  1846. case 1:
  1847. r=n;
  1848. g=v;
  1849. b=whiteness;
  1850. break;
  1851. case 2:
  1852. r=whiteness;
  1853. g=v;
  1854. b=n;
  1855. break;
  1856. case 3:
  1857. r=whiteness;
  1858. g=n;
  1859. b=v;
  1860. break;
  1861. case 4:
  1862. r=n;
  1863. g=whiteness;
  1864. b=v;
  1865. break;
  1866. case 5:
  1867. r=v;
  1868. g=whiteness;
  1869. b=n;
  1870. break;
  1871. default:
  1872. r=v;
  1873. g=n;
  1874. b=whiteness;
  1875. break;
  1876. }
  1877. return blendRGB(c, r, g, b);
  1878. }
  1879. }
  1880. /**************
  1881. * Lab
  1882. **************/
  1883. final static float D65X=0.950456;
  1884. final static float D65Y=1.0;
  1885. final static float D65Z=1.088754;
  1886. final static float CIEEpsilon=(216.0/24389.0);
  1887. final static float CIEK=(24389.0/27.0);
  1888. final static float CIEK2epsilon = CIEK * CIEEpsilon;
  1889. final static float D65FX_4 = 4.0*D65X/(D65X+15.0*D65Y+3.0*D65Z);
  1890. final static float D65FY_9 = 9.0*D65Y/(D65X+15.0*D65Y+3.0*D65Z);
  1891. final static float RANGE_X = 100.0 * (0.4124+0.3576+0.1805);
  1892. final static float RANGE_Y = 100.0;
  1893. final static float RANGE_Z = 100.0 * (0.0193+0.1192+0.9505);
  1894. final static float mepsilon = 1.0e-10;
  1895. final static float corrratio = 1.0/2.4;
  1896. final static float One_Third = 1.0/3.0;
  1897. final static float one_hsixteen = 1.0/116.0;
  1898. color toLAB(color c) {
  1899. PVector xyz = _toXYZ(getNR(c), getNG(c), getNB(c));
  1900. xyz.div(100.0);
  1901. xyz.x /= D65X;
  1902. xyz.y /= D65Y;
  1903. xyz.z /= D65Z;
  1904. float x, y, z;
  1905. if (xyz.x > CIEEpsilon) {
  1906. x = pow(xyz.x, One_Third);
  1907. } else {
  1908. x= (CIEK*xyz.x+16.0)*one_hsixteen;
  1909. }
  1910. if (xyz.y > CIEEpsilon) {
  1911. y = pow(xyz.y, One_Third);
  1912. } else {
  1913. y = (CIEK*xyz.y+16.0)*one_hsixteen;
  1914. }
  1915. if (xyz.z > CIEEpsilon) {
  1916. z = pow(xyz.z, One_Third);
  1917. } else {
  1918. z = (CIEK*xyz.z+16.0)*one_hsixteen;
  1919. }
  1920. float L = 255.0*(((116.0*y)-16.0)*0.01);
  1921. float a = 255.0*(0.5*(x-y)+0.5);
  1922. float b = 255.0*(0.5*(y-z)+0.5);
  1923. return blendRGB(c, round(L), round(a), round(b));
  1924. }
  1925. color fromLAB(color c) {
  1926. float L = 100*getNR(c);
  1927. float a = getNG(c)-0.5;
  1928. float b = getNB(c)-0.5;
  1929. float y = (L+16.0)*one_hsixteen;
  1930. float x = y+a;
  1931. float z = y-b;
  1932. float xxx=x*x*x;
  1933. if (xxx>CIEEpsilon) {
  1934. x = xxx;
  1935. } else {
  1936. x = (116.0*x-16.0)/CIEK;
  1937. }
  1938. float yyy=y*y*y;
  1939. if (yyy>CIEEpsilon) {
  1940. y = yyy;
  1941. } else {
  1942. y = L/CIEK;
  1943. }
  1944. float zzz=z*z*z;
  1945. if (zzz>CIEEpsilon) {
  1946. z = zzz;
  1947. } else {
  1948. z = (116.0*z-16.0)/CIEK;
  1949. }
  1950. return _fromXYZ(c, RANGE_X*x, RANGE_Y*y, RANGE_Z*z);
  1951. }
  1952. /**************
  1953. * Luv
  1954. **************/
  1955. final float PerceptibleReciprocal(float x) {
  1956. float sgn = x < 0.0 ? -1.0 : 1.0;
  1957. if ((sgn * x) >= mepsilon) return (1.0 / x);
  1958. return (sgn/mepsilon);
  1959. }
  1960. color toLUV(color c) {
  1961. PVector xyz = _toXYZ(getNR(c), getNG(c), getNB(c));
  1962. xyz.div(100.0);
  1963. float d = xyz.y; // / D65Y;
  1964. float L;
  1965. if (d > CIEEpsilon) L = 116.0*pow(d, One_Third)-16.0;
  1966. else L = CIEK * d;
  1967. float alpha = PerceptibleReciprocal(xyz.x + 15.0 * xyz.y + 3.0 * xyz.z);
  1968. float L13 = 13.0 * L;
  1969. float u = L13 * ((4.0 * alpha * xyz.x)-D65FX_4);
  1970. float v = L13 * ((9.0 * alpha * xyz.y)-D65FY_9);
  1971. L /= 100.0;
  1972. u=(u+134.0)/354.0;
  1973. v=(v+140.0)/262.0;
  1974. return blendRGB(c, round(L*255), round(u*255), round(v*255));
  1975. }
  1976. color fromLUV(color c) {
  1977. float L = 100.0*getNR(c);
  1978. float u = 354.0*getNG(c)-134.0;
  1979. float v = 262.0*getNB(c)-140.0;
  1980. float X, Y, Z;
  1981. if (L > CIEK2epsilon) Y = pow((L+16.0)*one_hsixteen, 3.0);
  1982. else Y = L/CIEK;
  1983. float L13 = 13.0*L;
  1984. float L52 = 52.0*L;
  1985. float Y5 = 5.0*Y;
  1986. float L13u = L52/(u+L13*D65FX_4);
  1987. X=((Y*((39.0*L/(v+L13*D65FY_9))-5.0))+Y5)/((((L13u)-1.0)/3.0)+One_Third);
  1988. Z=(X*(((L13u)-1.0)/3.0))-Y5;
  1989. return _fromXYZ(c, 100*X, 100*Y, 100*Z);
  1990. }
  1991. /**************
  1992. * HCL
  1993. **************/
  1994. color toHCL(color c) {
  1995. float r = getNR(c);
  1996. float g = getNG(c);
  1997. float b = getNB(c);
  1998. float max = max(r, max(g, b));
  1999. float chr = max - min(r, min(g, b));
  2000. float h = 0.0;
  2001. if ( chr != 0) {
  2002. if (r == max) {
  2003. h = ((g-b)/chr+6.0) % 6.0;
  2004. } else if (g == max) {
  2005. h = (b-r)/chr + 2.0;
  2006. } else {
  2007. h = (r-g)/chr + 4.0;
  2008. }
  2009. }
  2010. return blendRGB(c, round((h/6.0)*255), round(chr*255), round(255*(0.298839*r+0.586811*g+0.114350*b)));
  2011. }
  2012. color fromHCL(color c) {
  2013. float h = 6.0*getNR(c);
  2014. float chr = getNG(c);
  2015. float l = getNB(c);
  2016. float x = chr*(1.0-abs((h%2.0)-1.0));
  2017. float r = 0.0;
  2018. float g = 0.0;
  2019. float b = 0.0;
  2020. if ((0.0 <= h) && (h < 1.0)) {
  2021. r=chr;
  2022. g=x;
  2023. } else if ((1.0 <= h) && (h < 2.0)) {
  2024. r=x;
  2025. g=chr;
  2026. } else if ((2.0 <= h) && (h < 3.0)) {
  2027. g=chr;
  2028. b=x;
  2029. } else if ((3.0 <= h) && (h < 4.0)) {
  2030. g=x;
  2031. b=chr;
  2032. } else if ((4.0 <= h) && (h < 5.0)) {
  2033. r=x;
  2034. b=chr;
  2035. } else {//if ((5.0 <= h) && (h < 6.0)) {
  2036. r=chr;
  2037. b=x;
  2038. }
  2039. float m = l - (0.298839*r+0.586811*g+0.114350*b);
  2040. return blendRGB(c, round(255*(r+m)), round(255*(g+m)), round(255*(b+m)));
  2041. }
  2042. /**************
  2043. * Yxy
  2044. **************/
  2045. color toYXY(color c) {
  2046. PVector xyz = _toXYZ(getNR(c), getNG(c), getNB(c));
  2047. float sum = xyz.x + xyz.y + xyz.z;
  2048. float x = xyz.x > 0 ? xyz.x / sum : 0.0;
  2049. float y = xyz.y > 0 ? xyz.y / sum : 0.0;
  2050. return blendRGB(c,
  2051. (int)map(xyz.y, 0, RANGE_Y, 0, 255),
  2052. (int)map(x, 0.0, 1.0, 0, 255),
  2053. (int)map(y, 0.0, 1.0, 0, 255));
  2054. }
  2055. color fromYXY(color c) {
  2056. float Y = map(getR(c), 0, 255, 0, RANGE_Y);
  2057. float x = map(getG(c), 0, 255, 0, 1.0);
  2058. float y = map(getB(c), 0, 255, 0, 1.0);
  2059. float divy = Y / (y>0 ? y : 1.0e-6);
  2060. return _fromXYZ(c, x * divy, Y, (1-x-y)*divy);
  2061. }
  2062. /**************
  2063. * XYZ
  2064. **************/
  2065. // FIXME: range from 0 to 1
  2066. float correctionxyz(float n) {
  2067. return (n > 0.04045 ? pow((n + 0.055) / 1.055, 2.4) : n / 12.92) * 100.0;
  2068. }
  2069. PVector _toXYZ(float rr, float gg, float bb) {
  2070. float r = correctionxyz(rr);
  2071. float g = correctionxyz(gg);
  2072. float b = correctionxyz(bb);
  2073. return new PVector(r * 0.4124 + g * 0.3576 + b * 0.1805,
  2074. r * 0.2126 + g * 0.7152 + b * 0.0722,
  2075. r * 0.0193 + g * 0.1192 + b * 0.9505);
  2076. }
  2077. color toXYZ(color c) {
  2078. PVector xyz = _toXYZ(getNR(c), getNG(c), getNB(c));
  2079. return blendRGB(c,
  2080. (int)map(xyz.x, 0, RANGE_X, 0, 255),
  2081. (int)map(xyz.y, 0, RANGE_Y, 0, 255),
  2082. (int)map(xyz.z, 0, RANGE_Z, 0, 255));
  2083. }
  2084. float recorrectionxyz(float n) {
  2085. return n > 0.0031308 ? 1.055 * pow(n, corrratio) - 0.055 : 12.92 * n;
  2086. }
  2087. // FIXME: range from 0 to 1
  2088. color _fromXYZ(color c, float xx, float yy, float zz) {
  2089. float x = xx/100.0;
  2090. float y = yy/100.0;
  2091. float z = zz/100.0;
  2092. int r = round(255.0*recorrectionxyz(x * 3.2406 + y * -1.5372 + z * -0.4986));
  2093. int g = round(255.0*recorrectionxyz(x * -0.9689 + y * 1.8758 + z * 0.0415));
  2094. int b = round(255.0*recorrectionxyz(x * 0.0557 + y * -0.2040 + z * 1.0570));
  2095. return blendRGB(c, r, g, b);
  2096. }
  2097. color fromXYZ(color c) {
  2098. float x = map(getR(c), 0, 255, 0, RANGE_X);
  2099. float y = map(getG(c), 0, 255, 0, RANGE_Y);
  2100. float z = map(getB(c), 0, 255, 0, RANGE_Z);
  2101. return _fromXYZ(c, x, y, z);
  2102. }
  2103. /**************
  2104. * CMY
  2105. **************/
  2106. color toCMY(color c) {
  2107. return blendRGB(c, 255-getR(c), 255-getG(c), 255-getB(c));
  2108. }
  2109. color fromCMY(color c) {
  2110. return toCMY(c);
  2111. }
  2112. /**************
  2113. * OHTA
  2114. **************/
  2115. color fromOHTA(color c) {
  2116. int I1 = getR(c);
  2117. float I2 = map(getG(c), 0, 255, -127.5, 127.5);
  2118. float I3 = map(getB(c), 0, 255, -127.5, 127.5);
  2119. int R = (int)(I1+1.00000*I2-0.66668*I3);
  2120. int G = (int)(I1+1.33333*I3);
  2121. int B = (int)(I1-1.00000*I2-0.66668*I3);
  2122. return blendRGB(c, R, G, B);
  2123. }
  2124. color toOHTA(color c) {
  2125. int R = getR(c);
  2126. int G = getG(c);
  2127. int B = getB(c);
  2128. int I1 = (int)(0.33333*R+0.33334*G+0.33333*B);
  2129. int I2 = (int)map(0.5*(R-B), -127.5, 127.5, 0, 255);
  2130. int I3 = (int)map(-0.25000*R+0.50000*G-0.25000*B, -127.5, 127.5, 0, 255);
  2131. return blendRGB(c, I1, I2, I3);
  2132. }
  2133. ////
  2134. // 1/n table for n=0..255 - to speed up color conversions things
  2135. final static float[] r255 = {
  2136. 0.0, 0.003921569, 0.007843138, 0.011764706, 0.015686275, 0.019607844, 0.023529412, 0.02745098, 0.03137255, 0.03529412, 0.039215688,
  2137. 0.043137256, 0.047058824, 0.050980393, 0.05490196, 0.05882353, 0.0627451, 0.06666667, 0.07058824, 0.07450981, 0.078431375, 0.08235294,
  2138. 0.08627451, 0.09019608, 0.09411765, 0.09803922, 0.101960786, 0.105882354, 0.10980392, 0.11372549, 0.11764706, 0.12156863, 0.1254902,
  2139. 0.12941177, 0.13333334, 0.13725491, 0.14117648, 0.14509805, 0.14901961, 0.15294118, 0.15686275, 0.16078432, 0.16470589, 0.16862746,
  2140. 0.17254902, 0.1764706, 0.18039216, 0.18431373, 0.1882353, 0.19215687, 0.19607843, 0.2, 0.20392157, 0.20784314, 0.21176471, 0.21568628,
  2141. 0.21960784, 0.22352941, 0.22745098, 0.23137255, 0.23529412, 0.23921569, 0.24313726, 0.24705882, 0.2509804, 0.25490198, 0.25882354,
  2142. 0.2627451, 0.26666668, 0.27058825, 0.27450982, 0.2784314, 0.28235295, 0.28627452, 0.2901961, 0.29411766, 0.29803923, 0.3019608, 0.30588236,
  2143. 0.30980393, 0.3137255, 0.31764707, 0.32156864, 0.3254902, 0.32941177, 0.33333334, 0.3372549, 0.34117648, 0.34509805, 0.34901962, 0.3529412,
  2144. 0.35686275, 0.36078432, 0.3647059, 0.36862746, 0.37254903, 0.3764706, 0.38039216, 0.38431373, 0.3882353, 0.39215687, 0.39607844, 0.4,
  2145. 0.40392157, 0.40784314, 0.4117647, 0.41568628, 0.41960785, 0.42352942, 0.42745098, 0.43137255, 0.43529412, 0.4392157, 0.44313726,
  2146. 0.44705883, 0.4509804, 0.45490196, 0.45882353, 0.4627451, 0.46666667, 0.47058824, 0.4745098, 0.47843137, 0.48235294, 0.4862745, 0.49019608,
  2147. 0.49411765, 0.49803922, 0.5019608, 0.5058824, 0.50980395, 0.5137255, 0.5176471, 0.52156866, 0.5254902, 0.5294118, 0.53333336, 0.5372549,
  2148. 0.5411765, 0.54509807, 0.54901963, 0.5529412, 0.5568628, 0.56078434, 0.5647059, 0.5686275, 0.57254905, 0.5764706, 0.5803922, 0.58431375,
  2149. 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,
  2150. 0.6392157, 0.6431373, 0.64705884, 0.6509804, 0.654902, 0.65882355, 0.6627451, 0.6666667, 0.67058825, 0.6745098, 0.6784314, 0.68235296,
  2151. 0.6862745, 0.6901961, 0.69411767, 0.69803923, 0.7019608, 0.7058824, 0.70980394, 0.7137255, 0.7176471, 0.72156864, 0.7254902, 0.7294118,
  2152. 0.73333335, 0.7372549, 0.7411765, 0.74509805, 0.7490196, 0.7529412, 0.75686276, 0.7607843, 0.7647059, 0.76862746, 0.77254903, 0.7764706,
  2153. 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,
  2154. 0.83137256, 0.8352941, 0.8392157, 0.84313726, 0.84705883, 0.8509804, 0.85490197, 0.85882354, 0.8627451, 0.8666667, 0.87058824, 0.8745098,
  2155. 0.8784314, 0.88235295, 0.8862745, 0.8901961, 0.89411765, 0.8980392, 0.9019608, 0.90588236, 0.9098039, 0.9137255, 0.91764706, 0.92156863,
  2156. 0.9254902, 0.92941177, 0.93333334, 0.9372549, 0.9411765, 0.94509804, 0.9490196, 0.9529412, 0.95686275, 0.9607843, 0.9647059, 0.96862745,
  2157. 0.972549, 0.9764706, 0.98039216, 0.9843137, 0.9882353, 0.99215686, 0.99607843, 1.0
  2158. };