Instantly share code, notes, and snippets.

Embed
What would you like to do?
Very simple GPL loader for Godot Engine (2.1)
extends Node
export(String, FILE, '*.gpl') var palette
const HEADER = 'GIMP Palette'
const PARAM_NAME = 'Name:'
const PARAM_COLUMNS = 'Columns:'
const PARAMS_COUNT = 2
const COMMENT = '#'
var gpl
func _ready():
if palette != null:
generate_palette()
func generate_palette():
var pal = open_palette(palette)
gpl = parse_lines(pal.split('\n'))
return gpl
func open_palette(path):
var f = File.new()
f.open(path, File.READ)
return f.get_as_text()
func parse_lines(lines):
var params = {
'colors': [],
'names': []
}
var params_count = 0
var n = 0
for line in lines:
line = line.strip_edges()
if n == 0:
if line != HEADER:
print("Header error.")
elif params_count < PARAMS_COUNT:
params_count += 1
elif is_comment(line):
pass
else:
var color = get_color(line)
if color != null:
params['colors'].append(color[0])
params['names'].append(color[1])
n+=1
return params
func get_color(line):
line = line.replace('\t', ' ')
var color_info = line.split(' ')
while '' in color_info:
color_info.remove(Array(color_info).find(''))
if color_info.size() >= 4:
var color = Color(float(color_info[0])/255, float(color_info[1])/255, float(color_info[2])/255)
for i in range(3):
color_info.remove(0)
var name = ''
for w in color_info:
name += w+' '
name = name.strip_edges()
return [color, name]
func is_comment(line):
return line.begins_with(COMMENT)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment