document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. // write your code here
  2. /*
  3.  For each pixel of the black image that we create in the first line of the code, we calculate a number "value" in the
  4.  function calculate(pixel). Using a boolean xTrue the function uses two different functions to calculate the vlaue returned
  5.  by the function. In one state of the flag xTrue we use one function and in the other state of the boolean flag we use the
  6.  other function. Each time the function is called the state of the boolean variable is set to the opposite state. So the range
  7.  of "value" is between 0 and 255 excluding 255. The modulous operator keeps the "value" below 255 as the
  8.  allowed range  is 0-255 but excluding 255. Without the modulo the first function becomes
  9.   y+x*y which can return values in the range 0 to 65025.The function returns 65025 when x=254 AND y=255.
  10.   On the other hand we do not use the modulo operator in the second function (x+y/x) as even without the mod
  11.   it returns the values in the range 0 to 255 but excluding 255. Since in this funciton we are dividing y with x
  12.   before adding it to x therefore the total is always less than 255 and we do not need to use the mod %255 in this function.
  13.   This difference in the value of the two functions used give us the pattern in the resultant image.But the margins of the
  14.   picture are not marked so we use four different functions for the four borders of the image and using these functions we
  15.   can give distinct color tot he pixels just in these four borders. At this stage the picture is of greyish or blackish colors
  16.   so in order to change it the present color we use the function setColor. This function uses the value of the variable
  17.   "value" and finds out if any of the pixels x or y lies on the borders. If it lies on the border then it calls the
  18.   setBorders function otherwise it calls the setCenter function. The function isSpecialValue is used to make the border
  19.   on all the four sides 3 pixels wide and of different color to make it stand out in the image.
  20.    
  21.  * */
  22. var image = new SimpleImage(256, 256);
  23. var w = image.getWidth();
  24. var h = image.getHeight();
  25. var xTrue = true;
  26. var borderTrue = true;
  27.  
  28.  for (var pixel of image.values()) {
  29.     var value = calculate(pixel);
  30.     setColor(pixel, value);
  31.  }
  32.  print(image);
  33.  
  34.  function calculate(pixel){
  35.     var x = pixel.getX();
  36.     var y = pixel.getY();
  37.     var coordinate = 0;
  38.     if(xTrue){
  39.         coordinate = (y+x*y)%255;
  40.     }else{
  41.         coordinate = (x+y/x);
  42.     }
  43.     xTrue = !xTrue;
  44.     return coordinate;
  45.  }
  46.  
  47.  function setColor(pixel, value){
  48.      var x = pixel.getX();
  49.      var y = pixel.getY();
  50.      if(isSpecialValue(x, y)){
  51.         setBorders(pixel);
  52.      }else{
  53.         setCenter(pixel,x,y);
  54.      }
  55.  }
  56.  var isCenter = true;
  57.  function setCenter(pixel,x,y){
  58.      if(isCenter){
  59.         pixel.setRed(value/x);
  60.         pixel.setGreen(value);
  61.         pixel.setBlue(value/y);
  62.      }else{
  63.         pixel.setRed(value/y);
  64.         pixel.setGreen(value);
  65.         pixel.setBlue(value/x);
  66.      }
  67.     isCenter = !isCenter;
  68.  }
  69. function isSpecialValue(x,y){
  70.     if( y <= 3 || y >= h-3){
  71.         return true;
  72.     }else if(x <= 3 || x >= w-3){
  73.         return true;
  74.     }else{
  75.         return false;
  76.     }
  77. }
  78. function setBorders(pixel){
  79.     var x = pixel.getX();
  80.     var y = pixel.getY();
  81.     if(x == 0 || x == 1 || x == 2){
  82.         setLeftVerticalBorder(pixel,x,y);
  83.     }else if(x == w-3 || x == w-2 || x == w-1){
  84.         setRightVerticalBorder(pixel,x,y);
  85.     }else if(y == 0 || y == 1 || y == 2){
  86.         setTopHorizontalBorder(pixel,x,y);
  87.     }else if(y == h-3 || y == h-2 || y == h-1){
  88.         setBottomHorizontalBorder(pixel);
  89.     }
  90. }
  91. function setLeftVerticalBorder(pixel,x,y){
  92.     if(borderTrue){
  93.         pixel.setRed(255);
  94.         pixel.setGreen(0);
  95.         pixel.setBlue(0);
  96.     }else{
  97.         pixel.setRed(0);
  98.         pixel.setGreen(255);
  99.         pixel.setBlue(0);    
  100.     }
  101.     borderTrue = !borderTrue;
  102. }
  103. function setRightVerticalBorder(pixel,x,y){
  104.     if(borderTrue){
  105.         pixel.setRed(0);
  106.         pixel.setGreen(255);
  107.         pixel.setBlue(0);
  108.     }else{
  109.         pixel.setRed(255);
  110.         pixel.setGreen(0);
  111.         pixel.setBlue(0);    
  112.     }
  113.     borderTrue = !borderTrue;
  114. }
  115. function setTopHorizontalBorder(pixel,x,y){
  116.     if(borderTrue){
  117.         pixel.setRed(0);
  118.         pixel.setGreen(0);
  119.         pixel.setBlue(255);
  120.     }else{
  121.         pixel.setRed(255);
  122.         pixel.setGreen(0);
  123.         pixel.setBlue(0);    
  124.     }
  125.     borderTrue = !borderTrue;
  126. }
  127. function setBottomHorizontalBorder(pixel,x,y){
  128.     if(borderTrue){
  129.         pixel.setRed(255);
  130.         pixel.setGreen(0);
  131.         pixel.setBlue(0);
  132.     }else{
  133.         pixel.setRed(0);
  134.         pixel.setGreen(0);
  135.         pixel.setBlue(255);    
  136.     }
  137.     borderTrue = !borderTrue;
  138. }
');