Skip to content

Instantly share code, notes, and snippets.

@trpfrog
Last active June 28, 2019 15:21
Show Gist options
  • Select an option

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

Select an option

Save trpfrog/a4ea886ede54ca0cbdafe519a93f2a85 to your computer and use it in GitHub Desktop.
グラデーションを作ります
public class PictureMaker {
public static void main(String[] args){
Canvas canvas = new Canvas(10,10,255); //この時10×10pxの画像を出力します
RGB rgb = new RGB(0,0,0,255);
canvas.printHeader();
for (int y=0; y<canvas.getHeight(); y++) {
System.out.println();
rgb.setBlue( calcB(y,canvas) );
for (int x = 0; x < canvas.getWidth(); x++) {
rgb.setRed( calcR(x,y,canvas) );
rgb.setGreen( calcG(x,y,canvas) );
System.out.print(rgb+ " ");
}
}
}
private static int calcR (int x,int y, Canvas canvas){
int X = canvas.getWidth();
int Y = canvas.getHeight();
double result = 2 * Math.sqrt(
( ( Math.pow(x-(X/2),2) + Math.pow(y-(Y/2),2) ) )
/ ( ( Math.pow(X,2) + Math.pow(Y,2) ) )
);
result *= canvas.getMaxRGB();
return (int) Math.round(result);
}
private static int calcG (int x,int y, Canvas canvas){
int X = canvas.getWidth();
int Y = canvas.getHeight();
double result = Math.sqrt(
( Math.pow(x,2) + Math.pow(y,2) ) / ( Math.pow(X,2) + Math.pow(Y,2) )
);
result *= canvas.getMaxRGB();
return (int) Math.round(result);
}
private static int calcB (int y, Canvas canvas){
int Y = canvas.getHeight();
double result = (double) y/Y;
result *= canvas.getMaxRGB();
return (int) Math.round(result);
}
}
class RGB{
private int red;
private int green;
private int blue;
private int maxRGB;
public RGB(int red, int green, int blue, int maxRGB) {
this.red = red;
this.green = green;
this.blue = blue;
this.maxRGB = maxRGB;
}
public void setRed(int red) { this.red = this.limit(red); }
public void setGreen(int green) { this.green = this.limit(green); }
public void setBlue(int blue) { this.blue = this.limit(blue); }
private int limit(int i){
return i % (this.maxRGB+1);
}
@Override
public String toString() { return this.red+" "+this.green+" "+this.blue; }
}
class Canvas{
private int width;
private int height;
private int maxRGB = 0;
public Canvas(int width, int height,int maxRGB) {
this.width = width;
this.height = height;
this.maxRGB = maxRGB;
}
public int getWidth() { return this.width; }
public int getHeight() { return this.height; }
public int getMaxRGB() { return this.maxRGB; }
public void printHeader(){
System.out.print("P3 "+this.width+" "+this.height+" "+this.maxRGB);
}
}
P3 10 10 255
255 0 0 231 18 0 210 36 0 194 54 0 184 72 0 180 90 0 184 108 0 194 126 0 210 144 0 231 162 0
231 18 26 204 26 26 180 40 26 161 57 26 149 74 26 144 92 26 149 110 26 161 128 26 180 145 26 204 163 26
210 36 51 180 40 51 153 51 51 130 65 51 114 81 51 108 97 51 114 114 51 130 131 51 153 149 51 180 166 51
194 54 77 161 57 77 130 65 77 102 77 77 81 90 77 72 105 77 81 121 77 102 137 77 130 154 77 161 171 77
184 72 102 149 74 102 114 81 102 81 90 102 51 102 102 36 115 102 51 130 102 81 145 102 114 161 102 149 178 102
180 90 128 144 92 128 108 97 128 72 105 128 36 115 128 0 128 128 36 141 128 72 155 128 108 170 128 144 186 128
184 108 153 149 110 153 114 114 153 81 121 153 51 130 153 36 141 153 51 153 153 81 166 153 114 180 153 149 195 153
194 126 179 161 128 179 130 131 179 102 137 179 81 145 179 72 155 179 81 166 179 102 179 179 130 192 179 161 206 179
210 144 204 180 145 204 153 149 204 130 154 204 114 161 204 108 170 204 114 180 204 130 192 204 153 204 204 180 217 204
231 162 230 204 163 230 180 166 230 161 171 230 149 178 230 144 186 230 149 195 230 161 206 230 180 217 230 204 230 230
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment