Skip to content

Instantly share code, notes, and snippets.

@trpfrog
Created June 22, 2019 21:15
Show Gist options
  • Select an option

  • Save trpfrog/fab660d32c83ace2d0848e04f5536ffa to your computer and use it in GitHub Desktop.

Select an option

Save trpfrog/fab660d32c83ace2d0848e04f5536ffa to your computer and use it in GitHub Desktop.
ppmファイルで使うグラデーション作るやつ
public class MakePicture {
public static void main(String[] args){
final int WIDTH = 255;
final int HEIGHT = 255;
RGB rgb = new RGB();
System.out.println("P3 255 "+WIDTH+" "+HEIGHT);
printRedBlueGradation(rgb,WIDTH,HEIGHT);
}
private static void printRedBlueGradation(RGB rgb, int width, int height){
StringBuilder sb;
for (int i=1;i<height;i++) {
sb = new StringBuilder();
for (int j = 1; j <= width; j++) {
sb.append(rgb);
sb.append(" ");
rgb.addBlue(1);
}
System.out.println(sb);
rgb.setBlue(0);
rgb.addRed(1);
}
}
}
class RGB{
private int red = 0;
private int green = 0;
private int blue = 0;
private boolean doAutoCast = true;
private static final int MAXRGB = 255;
public RGB(int red, int green, int blue) {
this.red = red;
this.green = green;
this.blue = blue;
}
public RGB() {
this.red = 0;
this.green = 0;
this.blue = 0;
}
public void setAutoCast(boolean b) {this.doAutoCast = b;}
public int getRed() { return red; }
public int getGreen() { return green; }
public int getBlue() { return blue; }
public void setRed(int red) { this.red = this.cast(red); }
public void setGreen(int green) { this.green = this.cast(green); }
public void setBlue(int blue) { this.blue = this.cast(blue); }
public void addRed(int red) { this.red = this.cast(this.red + red); }
public void addGreen(int green) { this.green = this.cast(this.green + green); }
public void addBlue(int blue) { this.blue = this.cast(this.blue + blue); }
private int cast(int i){
if(this.doAutoCast) {
while (i < 0 || MAXRGB < i) {
if (i < 0) {
i += MAXRGB+1; //0〜255で合計256なので1を足す
} else {
i -= MAXRGB+1; //同上
}
}
return i;
}else{
throw new IllegalArgumentException();
}
}
@Override
public String toString() { return this.red+" "+this.green+" "+this.blue; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment