@tool extends Node func extract_color_dict(input_texture: Texture2D) -> Dictionary: var color_dict: Dictionary[Color, int] var image: Image = input_texture.get_image() var x = image.get_width() var y = image.get_height() for nx in x: for ny in y: var color: Color = image.get_pixel(nx, ny) if with_transparency == false: if color.a == 1.0: if color_dict.has(color): color_dict[color] += 1 else: color_dict.get_or_add(color, 1) else: if color_dict.has(color): color_dict[color] += 1 else: color_dict.get_or_add(color, 1) return color_dict func extract_color(input_texture: Texture2D) -> Color: var output_color: Color var dict: Dictionary = extract_color_dict(input_texture) var color_array: Array[Color] = dict.keys() var probabilities: Array[int] = dict.values() colors = dict.duplicate() var random = RandomNumberGenerator.new() var weighted_array = PackedFloat32Array(probabilities) output_color = (color_array[random.rand_weighted(weighted_array)]) return output_color func random_color(input_colors: Array[Color])-> Color: var c: Color = input_colors.pick_random() return c @export var with_transparency: bool @export var test_texture: Texture2D @export var colors: Dictionary[Color, int] @export var picked_color_by_weight: Color @export var picked_color_random: Color @export_tool_button("Check colors") var tool_button: Callable = _tool_button func _tool_button(): colors.clear() picked_color_by_weight = Color.BLACK var c = extract_color(test_texture) picked_color_by_weight = c picked_color_random = random_color(colors.keys())
or share this direct link: