What's new

Closed Javascript (for loop) changing color from black to yellow

Status
Not open for further replies.

chad0926

Enthusiast
Joined
Jul 21, 2018
Posts
6
Reaction
2
Points
63
Hi,

I'm new here and I'm also starting to learn programming from scratch (non-IT grad). Hoping to switch career from teaching to programming.

HELP!

How can I change a 200x200 black image to yellow using Javascript's For Loop? Nalilito kasi ako sa "formula".
Eto ang naisip ko. Black is (0,0,0) while yellow is (255,255,0).

var img = new SimpleImage(200,200);
print(img);

for (var pixel of img.values()){
var newG = 255 - pixel.getGreen();
var newR = 255 - pixel.getRed();
var newB = 0 - pixel.getBlue();
pixel.setGreen(newG);
pixel.setRed(newR);
pixel.setBlue(newB);
}

Thanks for the help!
 
let r = 255;
let g = 255;
let b = 0;

for (let pixel of img.values()){
pixel.setRed(r);
pixel.setGreen(g);
pixel.setBlue(b);
}
 
kita ko na ginamit mo library, try mo to:

let canvas = document.getElementById('myCanvas');

let img = new SimpleImage(200,200);
let r = 255;
let g = 255;
let b = 0;

for (let x = 0; x < 200; x++){
for (let y = 0; y < 200; y++){
img.setRed(x,y,r);
img.setGreen(x,y,g);
img.setBlue(x,y,b);

}
}

img.drawTo(canvas);
 
kita ko na ginamit mo library, try mo to:

let canvas = document.getElementById('myCanvas');

let img = new SimpleImage(200,200);
let r = 255;
let g = 255;
let b = 0;

for (let x = 0; x < 200; x++){
for (let y = 0; y < 200; y++){
img.setRed(x,y,r);
img.setGreen(x,y,g);
img.setBlue(x,y,b);

}
}

img.drawTo(canvas);

Ayaw pa rin e.
Error:
Identifier 'img' has already been declared
 
Ayaw pa rin e.
Error:
Identifier 'img' has already been declared
LAST
delete mo lahat ng code dyan sa box bago i paste.


let img = new SimpleImage(200,200);
let r = 255;
let g = 255;
let b = 0;


for (let pixel of img.values()){
pixel.setRed(r);
pixel.setGreen(g);
pixel.setBlue(b);
}

print(img);
 
LAST
delete mo lahat ng code dyan sa box bago i paste.


let img = new SimpleImage(200,200);
let r = 255;
let g = 255;
let b = 0;


for (let pixel of img.values()){
pixel.setRed(r);
pixel.setGreen(g);
pixel.setBlue(b);
}

print(img);

Sa wakas! Salamat Wolfrain14. Dami ko pa kelangan alamin.
 
Status
Not open for further replies.

Similar threads

Back
Top