# colorillusion.frink

``` use Writer.frink // This program generates color illusions like this: // http://www.johnsadowski.com/big_spanish_castle.php inputURL = "file:DSC_0077.JPG" img_in = new image[inputURL] // Get file part of URL to use for output files. origFile = urlFile[inputURL] greyFile = origFile greyFile =~  %s/(.*)\.\w+\$/\$1grey.jpg/ colorFile = origFile colorFile =~ %s/(.*)\.\w+\$/\$1colors.jpg/ htmlFile = origFile htmlFile  =~ %s/(.*)\.\w+\$/\$1.html/ Width = img_in.getWidth[] Height = img_in.getHeight[] reverseImg = new image[Width, Height] greyImg = new image[Width, Height] for w = 0 to Width-1 {    for h = 0 to Height-1    {       [r,g,b] = img_in.getPixel[w, h]       // This converts to YUV space using the BT. 709 definition used in       // HDTV.  See http://en.wikipedia.org/wiki/YUV       y =  0.2126  r + 0.7152  g + 0.0722  b       u = -0.09991 r - 0.33609 g + 0.436   b       v =  0.615   r - 0.55861 g - 0.05639 b       // Write out the grayscale levels.       greyImg.setPixel[w, h, y, y, y]       // Now we're going the throw away the Y (luminance) component       // and replace it with a constant 0.5.  Then we're going to go back to       // RGB space and reverse the colors.  We could simplify these equations       // but it's kinda nice to see them in this form.       y = 0.5       r = y +       0 u + 1.28033 v       g = y - 0.21482 u - 0.38059 v       b = y + 2.12798 u -       0 v       reverseImg.setPixel[w, h, 1-r, 1-g, 1-b]    } } g = new graphics g.fitCenter[reverseImg, 0, 0, 1, 1] g.drawEllipseCenter[0,0,0.005, 0.005] //win = g.show[Width, Height] g.write[colorFile, Width, Height] g1 = new graphics g1.fitCenter[greyImg, 0, 0, 1, 1] g1.drawEllipseCenter[0,0,0.005, 0.005] g1.write[greyFile, Width, Height] // Render an interactive HTML file println["htmlFile is \$htmlFile"] html = new Writer[htmlFile] html.write["""<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"    "http://www.w3.org/TR/html4/loose.dtd"> <HTML LANG="en">  <HEAD>   <TITLE>\$htmlFile</TITLE>  </HEAD>  <BODY>   <CENTER>    <P>     Stare at the dot for 30 seconds, then move your mouse over the image.    </P>    <P>    <IMG NAME="i1" SRC="\$colorFile" onMouseOver="document.i1.src='\$greyFile'" onMouseOut="document.i1.src='\$colorFile'">    </P>   </CENTER>  </BODY> </HTML>"""] html.close[] println["Interactive file rendered to \$htmlFile"] // Start the interactive HTML file in the browser. browse[htmlFile] /*for i = 1 to 10 {    win.replaceGraphics[g1]    sleep[5 s]    win.replaceGraphics[g]    sleep[30 s] }*/ ```