Skip to content

Commit

Permalink
feat(image_lib.gd)!: 添加主题、配色版和相关函数
Browse files Browse the repository at this point in the history
新增:

- static var theme: String = "blue" 表示当前主题。
- Color get_palette_color_by_name(name) 获取当前主题下代号 name 的颜色。
- Color get_palette_color_by_info(col_theme, name) 获取主题 col_theme 下
  代号 name 的颜色。
- 其他函数和常量。

修改:

- PALETTE 修改:PALETTE[col_theme][name]: Color 现在表示主题 col_theme
  下代号 name 的颜色。
- void update_animation(...) 函数修改:现在该函数会先替换颜色,再根据
  当前 theme 替换颜色(若颜色不在 PALETTE 中则不替换)。

BREAKING CHANGE: 原 PALETTE 废用,update_animation 函数更改。

原先使用 PALETTE 的情况下,考虑用 get_palette_color_by_name 或
get_palette_color_by_info 替代。
  • Loading branch information
cutekibry committed Feb 16, 2024
1 parent c636a1f commit c1575a9
Showing 1 changed file with 91 additions and 26 deletions.
117 changes: 91 additions & 26 deletions scripts/image_lib.gd
Original file line number Diff line number Diff line change
@@ -1,29 +1,60 @@
class_name ImageLib


## 不同主题下的配色模板。
## [br][br]
## [code]PALETTE[theme][name]: Color[/code] 表示配色主题 [param theme] 下代号为 [param name] 的颜色值。
## [br][br]
## [param theme] 和 [param name] 的取值分别参见 [member COLOR_THEMES] 和 [member COLOR_NAMES]。
const PALETTE = {
"red": Color("#dd4132"),
"golden": Color("#f5df4d"),

"orange": Color("#ffb463"),
"card-op": Color("#ffb463"),
"skyblue": Color("#c2fdff"),
"card-bracl": Color("#c2fdff"),
"card-bracr": Color("#c2fdff"),

"lightblue": Color("#8eacf3"),
"card-var": Color("#8eacf3"),

"lightgreen": Color("#8ff297"),
"card-comp": Color("#8ff297"),
"sliver": Color("#f0f0f0"),
"card-const": Color("#f0f0f0"),

"default": Color.BLACK,
"blue": {
"lightest": Color("#25acf5"),
"light": Color("#1793e6"),
"mid": Color("#195ba6"),
"darkest": Color("#243966"),

"golden": Color("#f5df4d"),
"black": Color.BLACK,
"red": Color("#dd4132"),
},
"green": {
"lightest": Color("#94bf30"),
"light": Color("#55b33b"),
"mid": Color("#068051"),
"darkest": Color("#116061"),

"golden": Color("#f5df4d"),
"black": Color.BLACK,
"red": Color("#dd4132"),
}
}

static func change_color(image: Texture2D, old_color: Color, new_color: Color) -> Texture2D:

const COLOR_THEMES := ["blue", "green"] ## 可选的配色主题 [code]theme[/code] 取值。

const COLOR_NAMES := ["lightest", "light", "mid", "darkest", "golden", "black", "red"] ## 可选的颜色代号 [code]name[/code] 取值。



static var theme := "blue" ## 当前主题。



## 获取 [param color] 的主题色 [code]theme[/code] 和代号 [code]name[/code]。
## [br][br]
## 若没有在 [member PALETTE] 里出现,返回 [code]null[/code]。
## [br][br]
## 否则返回 [code][theme, name][/code]。
static func get_color_info(color: Color) -> Variant:
for col_theme in PALETTE.keys():
for name in PALETTE[col_theme].keys():
if color == PALETTE[col_theme][name]:
return [col_theme, name]
return null


## 将 [param image] 中的 [Color] [param old_color] 替换为 [Color] [param new_color]。
static func replace_color(image: Texture2D, old_color: Color, new_color: Color) -> Texture2D:
var new_texture = image.get_image()
for x in range(new_texture.get_width()):
for y in range(new_texture.get_height()):
Expand All @@ -32,19 +63,53 @@ static func change_color(image: Texture2D, old_color: Color, new_color: Color) -
new_texture.set_pixel(x, y, new_color)
return ImageTexture.create_from_image(new_texture)


## 将 [param image] 中,在 [member PALETTE] 出现过的 [Color] 替换为当前配色主题下相同代号的 [Color]。
static func replace_palette_colors_in_image(image: Texture2D) -> Texture2D:
var new_texture = image.get_image()
for x in range(new_texture.get_width()):
for y in range(new_texture.get_height()):
var info = get_color_info(new_texture.get_pixel(x, y))
if info != null:
new_texture.set_pixel(x, y, PALETTE[theme][info[1]])
return ImageTexture.create_from_image(new_texture)


static func update_animation(
sprite: AnimatedSprite2D,
start: int,
n: int,
step: int,
path_pattern: String,
old_color: Color,
new_color: Color
old_color: Color = Color.BLACK,
new_color: Color = Color.BLACK
) -> void:
var animation_id = "%d_%d_%d_%s" % [start, n, step, new_color.to_html()]
var animation_id = "%d_%d_%d_%s_%s_%s" % [start, n, step, path_pattern, theme, new_color.to_html()]
if not sprite.sprite_frames.has_animation(animation_id):
sprite.sprite_frames.add_animation(animation_id)
for i in range(n):
var image = load(path_pattern % (i * step + start))
sprite.sprite_frames.add_frame(animation_id, ImageLib.change_color(image, old_color, new_color))
var image
if path_pattern.contains("%d"):
image = load(path_pattern % (i * step + start))
else:
image = load(path_pattern)
if old_color != new_color:
image = ImageLib.replace_color(image, old_color, new_color)
image = ImageLib.replace_palette_colors_in_image(image)
sprite.sprite_frames.add_frame(animation_id, image)
sprite.play(animation_id)


## 获取当前主题下,代号为 [param name] 的颜色值。
## [br][br]
## [param name] 的取值参见 [member COLOR_NAMES]。
static func get_palette_color_by_name(name: String) -> Color:
return PALETTE[theme][name]



## 获取主题 [param col_theme] 下,代号为 [param name] 的颜色值。
## [br][br]
## [param col_theme] 和 [param name] 的取值参见 [member COLOR_THEMES] 和 [member COLOR_NAMES]。
static func get_palette_color_by_info(col_theme: String, name: String) -> Color:
return PALETTE[col_theme][name]

0 comments on commit c1575a9

Please sign in to comment.