Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // write your code here
- /*
- For each pixel of the black image that we create in the first line of the code, we calculate a number "value" in the
- function calculate(pixel). Using a boolean xTrue the function uses two different functions to calculate the vlaue returned
- 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
- other function. Each time the function is called the state of the boolean variable is set to the opposite state. So the range
- of "value" is between 0 and 255 excluding 255. The modulous operator keeps the "value" below 255 as the
- allowed range is 0-255 but excluding 255. Without the modulo the first function becomes
- y+x*y which can return values in the range 0 to 65025.The function returns 65025 when x=254 AND y=255.
- On the other hand we do not use the modulo operator in the second function (x+y/x) as even without the mod
- it returns the values in the range 0 to 255 but excluding 255. Since in this funciton we are dividing y with x
- 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.
- This difference in the value of the two functions used give us the pattern in the resultant image.But the margins of the
- picture are not marked so we use four different functions for the four borders of the image and using these functions we
- can give distinct color tot he pixels just in these four borders. At this stage the picture is of greyish or blackish colors
- so in order to change it the present color we use the function setColor. This function uses the value of the variable
- "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
- setBorders function otherwise it calls the setCenter function. The function isSpecialValue is used to make the border
- on all the four sides 3 pixels wide and of different color to make it stand out in the image.
- * */
- var image = new SimpleImage(256, 256);
- var w = image.getWidth();
- var h = image.getHeight();
- var xTrue = true;
- var borderTrue = true;
- for (var pixel of image.values()) {
- var value = calculate(pixel);
- setColor(pixel, value);
- }
- print(image);
- function calculate(pixel){
- var x = pixel.getX();
- var y = pixel.getY();
- var coordinate = 0;
- if(xTrue){
- coordinate = (y+x*y)%255;
- }else{
- coordinate = (x+y/x);
- }
- xTrue = !xTrue;
- return coordinate;
- }
- function setColor(pixel, value){
- var x = pixel.getX();
- var y = pixel.getY();
- if(isSpecialValue(x, y)){
- setBorders(pixel);
- }else{
- setCenter(pixel,x,y);
- }
- }
- var isCenter = true;
- function setCenter(pixel,x,y){
- if(isCenter){
- pixel.setRed(value/x);
- pixel.setGreen(value);
- pixel.setBlue(value/y);
- }else{
- pixel.setRed(value/y);
- pixel.setGreen(value);
- pixel.setBlue(value/x);
- }
- isCenter = !isCenter;
- }
- function isSpecialValue(x,y){
- if( y <= 3 || y >= h-3){
- return true;
- }else if(x <= 3 || x >= w-3){
- return true;
- }else{
- return false;
- }
- }
- function setBorders(pixel){
- var x = pixel.getX();
- var y = pixel.getY();
- if(x == 0 || x == 1 || x == 2){
- setLeftVerticalBorder(pixel,x,y);
- }else if(x == w-3 || x == w-2 || x == w-1){
- setRightVerticalBorder(pixel,x,y);
- }else if(y == 0 || y == 1 || y == 2){
- setTopHorizontalBorder(pixel,x,y);
- }else if(y == h-3 || y == h-2 || y == h-1){
- setBottomHorizontalBorder(pixel);
- }
- }
- function setLeftVerticalBorder(pixel,x,y){
- if(borderTrue){
- pixel.setRed(255);
- pixel.setGreen(0);
- pixel.setBlue(0);
- }else{
- pixel.setRed(0);
- pixel.setGreen(255);
- pixel.setBlue(0);
- }
- borderTrue = !borderTrue;
- }
- function setRightVerticalBorder(pixel,x,y){
- if(borderTrue){
- pixel.setRed(0);
- pixel.setGreen(255);
- pixel.setBlue(0);
- }else{
- pixel.setRed(255);
- pixel.setGreen(0);
- pixel.setBlue(0);
- }
- borderTrue = !borderTrue;
- }
- function setTopHorizontalBorder(pixel,x,y){
- if(borderTrue){
- pixel.setRed(0);
- pixel.setGreen(0);
- pixel.setBlue(255);
- }else{
- pixel.setRed(255);
- pixel.setGreen(0);
- pixel.setBlue(0);
- }
- borderTrue = !borderTrue;
- }
- function setBottomHorizontalBorder(pixel,x,y){
- if(borderTrue){
- pixel.setRed(255);
- pixel.setGreen(0);
- pixel.setBlue(0);
- }else{
- pixel.setRed(0);
- pixel.setGreen(0);
- pixel.setBlue(255);
- }
- borderTrue = !borderTrue;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement