用Java将butterfly渐变色输出为图像

yaml配置项

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# When cover is not set, the default cover is displayed (當沒有設置cover時,默認的封面顯示)
default_cover:
- 'linear-gradient(20deg, #0062be, #925696, #cc426e, #fb0347)' # default
- 'linear-gradient(210deg, #ff9a9e, #fad0c4, #fad0c4)' # 浪漫粉红
- 'linear-gradient(180deg, #232526, #414345, #5e6062)' # 神秘夜空
- 'linear-gradient(165deg, #56ab2f, #a8e063, #76ba1b)' # 热带雨林
- 'linear-gradient(150deg, #accbee, #e7f0fd, #ffffff)' # 霜冻早晨
- 'linear-gradient(135deg, #e6e9af, #e8c37e, #f4d03f)' # 金色沙滩
- 'linear-gradient(120deg, #DA22FF, #9733EE, #65799B, #5E2563)' # 梦幻紫罗兰
- 'linear-gradient(90deg, #0f2027, #203a43, #2c5364)' # 深邃宇宙
- 'linear-gradient(75deg, #9be15d, #00e3ae, #88d8b0)' # 清新草原
- 'linear-gradient(60deg, #ff7e5f, #feb47b, #ff7e5f, #fcb045)' # 日落黄昏
- 'linear-gradient(45deg, #2e8b57, #48d1cc, #00ced1, #7fffd4)' # 冷静海洋风
- 'linear-gradient(165deg, #3e5151, #decba4)' # 静谧森林
- 'linear-gradient(150deg, #2E3192, #1BFFFF)' # 神秘海域
- 'linear-gradient(135deg, #ee9ca7, #ffdde1)' # 晚霞光影
- 'linear-gradient(120deg, #FF5F6D, #FFC371)' # 晨曦微光
- 'linear-gradient(105deg, #000428, #004e92)' # 寒冬极光
- 'linear-gradient(90deg, #D9AFD9, #97D9E1)' # 草原日落
- 'linear-gradient(75deg, #0f0c29, #302b63, #24243e)' # 星光银河
- 'linear-gradient(60deg, #C9FFBF, #FFAFBD, #FFF)' # 清晨露水
- 'linear-gradient(45deg, #FFB75E, #ED8F03)' # 暖阳橙黄
- 'linear-gradient(30deg, #5f2c82, #49a09d)' # 宁静蓝天

java源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package org.example;

import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;

public class GradientImage {
public static void main(String[] args) throws Exception {
int width = 400;
int height = 200;

// 默认渐变色
saveGradientImage("#0062be", "#925696", "#cc426e", "#fb0347", "default");

// 浪漫粉红
saveGradientImage("#ff9a9e", "#fad0c4", "#fad0c4", null, "romantic_pink");

// 神秘夜空
saveGradientImage("#232526", "#414345", "#5e6062", null, "mysterious_night");

// 热带雨林
saveGradientImage("#56ab2f", "#a8e063", "#76ba1b", null, "tropical_rainforest");

// 霜冻早晨
saveGradientImage("#accbee", "#e7f0fd", "#ffffff", null, "frosty_morning");

// 金色沙滩
saveGradientImage("#e6e9af", "#e8c37e", "#f4d03f", null, "golden_beach");

// 梦幻紫罗兰
saveGradientImage("#DA22FF", "#9733EE", "#65799B", "#5E2563", "dreamy_violet");

// 深邃宇宙
saveGradientImage("#0f2027", "#203a43", "#2c5364", null, "deep_space");

// 清新草原
saveGradientImage("#9be15d", "#00e3ae", "#88d8b0", null, "fresh_meadow");

// 日落黄昏
saveGradientImage("#ff7e5f", "#feb47b", "#ff7e5f", "#fcb045", "sunset_dusk");

// 冷静海洋风
saveGradientImage("#2e8b57", "#48d1cc", "#00ced1", "#7fffd4", "calm_ocean_breeze");

// 静谧森林
saveGradientImage("#3e5151", "#decba4", null, null, "serene_forest");

// 神秘海域
saveGradientImage("#2E3192", "#1BFFFF", null, null, "mysterious_sea");

// 晚霞光影
saveGradientImage("#ee9ca7", "#ffdde1", null, null, "evening_glow");

// 晨曦微光
saveGradientImage("#FF5F6D", "#FFC371", null, null, "morning_glow");

// 寒冬极光
saveGradientImage("#000428", "#004e92", null, null, "winter_aurora");

// 草原日落
saveGradientImage("#D9AFD9", "#97D9E1", null, null, "prairie_sunset");

// 星光银河
saveGradientImage("#0f0c29", "#302b63", "#24243e", null, "starry_galaxy");

// 清晨露水
saveGradientImage("#C9FFBF", "#FFAFBD", "#FFF", null, "morning_dew");

// 暖阳橙黄
saveGradientImage("#FFB75E", "#ED8F03", null, null, "warm_sunshine");

// 宁静蓝天
saveGradientImage("#5f2c82", "#49a09d", null, null, "tranquil_blue_sky");
}

private static void saveGradientImage(String color1, String color2, String color3, String color4, String imageName)
throws Exception {
int width = 400;
int height = 200;

// 创建渐变色
GradientPaint gradient;

if (color4 != null) {
gradient = new GradientPaint(0, 0, Color.decode(color1),
width, height, Color.decode(color4));
} else if (color3 != null) {
gradient = new GradientPaint(0, 0, Color.decode(color1),
width, height, Color.decode(color3));
} else {
gradient = new GradientPaint(0, 0, Color.decode(color1),
width, height, Color.decode(color2));
}

// 创建 BufferedImage 对象
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

// 获取 Graphics2D 对象
Graphics2D g2d = image.createGraphics();

// 应用渐变色
g2d.setPaint(gradient);
g2d.fillRect(0, 0, width, height);

// 保存图片
ImageIO.write(image, "png", new File("/Users/raccoon/Desktop/Wallpapers/400x200/" + imageName + ".png"));
}
}

400x400

default

calm_ocean_breeze

deep_space

dreamy_violet

evening_glow

fresh_meadow

frosty_morning

golden_beach

morning_dew

morning_glow

mysterious_night

mysterious_sea

prairie_sunset

romantic_pink

serene_forest

starry_galaxy

sunset_dusk

tranquil_blue_sky

tropical_rainforest

warm_sunshine

winter_aurora

400x200

evening_glow

calm_ocean_breeze

deep_space

default

dreamy_violet

fresh_meadow

frosty_morning

golden_beach

morning_dew

morning_glow

mysterious_night

mysterious_sea

prairie_sunset

romantic_pink

serene_forest

starry_galaxy

sunset_dusk

tranquil_blue_sky

tropical_rainforest

warm_sunshine

winter_aurora