Created
October 12, 2016 17:56
-
-
Save Eiyeron/ba8b50346700533ce053b7e65bc8b166 to your computer and use it in GitHub Desktop.
Palette class creation utility macro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package colors; | |
import haxe.macro.Context; | |
import haxe.macro.Expr; | |
using haxe.macro.ExprTools; | |
/** | |
* Macro building a palette class from a given string list and a prefix. | |
* Creates static fields in the class named like "prefix_XXX" where XXX is the index | |
* of the color with leading zeros. Also creates an array containing every color. | |
* @author Eiyeron | |
* | |
* Here's how to create Arne's Famicube palette with said macro | |
* @example | |
@:build(colors.PaletteCreator.build("fmcube",[ | |
"#000000", "#00177D", "#024ACA", "#0084FF", | |
"#5BA8FF", "#98DCFF", "#9BA0EF", "#6264DC", | |
"#3D34A5", "#211640", "#5A1991", "#6A31CA", | |
"#A675FE", "#E2C9FF", "#FEC9ED", "#D59CFC", | |
"#CC69E4", "#A328B3", "#871646", "#CF3C71", | |
"#FF82CE", "#FFE9C5", "#F5B784", "#E18289", | |
"#DA655E", "#823C3D", "#4F1507", "#E03C28", | |
"#E2D7B5", "#C59782", "#AE6C37", "#5C3C0D", | |
"#231712", "#AD4E1A", "#F68F37", "#FFE737", | |
"#FFBB31", "#CC8F15", "#939717", "#B6C121", | |
"#EEFFA9", "#BEEB71", "#8CD612", "#6AB417", | |
"#376D03", "#172808", "#004E00", "#139D08", | |
"#58D332", "#20B562", "#00604B", "#005280", | |
"#0A98AC", "#25E2CD", "#BDFFCA", "#71A6A1", | |
"#415D66", "#0D2030", "#151515", "#343434", | |
"#7B7B7B", "#A8A8A8", "#D7D7D7", "#FFFFFF"])) | |
class Famicube {} | |
* | |
* @notes I almost have no frigging clue on how Macros works and the documentation isn't | |
* clear enough. | |
*/ | |
class PaletteCreator { | |
public static macro function build(prefix:String, palette:Array<String>):Array<Field> { | |
// Small function to add enough zeroes to have something like 001, 002... 125... | |
function add_leading_zeros(value:Int):String | |
{ | |
var str = ""; | |
var logMax = Math.floor(Math.log(palette.length == 0 ? 1 : palette.length)/Math.log(10)); | |
var logI = Math.floor(Math.log(value)/Math.log(10)); | |
for(i in 0...logMax-logI) | |
str += "0"; | |
return str+value; | |
} | |
// Getting the class fields | |
var fields = Context.getBuildFields(); | |
// Creating the expression array used to create the palette array. | |
var palette_var = []; | |
// For every value in the palette, create a field. | |
for(i in 0...palette.length){ | |
var v = { | |
name: prefix+add_leading_zeros(i), | |
access: [Access.APublic, Access.AStatic], | |
kind: FieldType.FVar(macro:flixel.util.FlxColor, | |
macro flixel.util.FlxColor.fromString($v{palette[i]})), | |
pos: Context.currentPos(), | |
}; | |
fields.push(v); | |
// Push an identifier reference to the palette array | |
palette_var.push(macro $i{v.name}); | |
} | |
// Create the palette array field | |
fields.push({ | |
name:"palette", | |
access: [Access.APublic, Access.AStatic], | |
// Generates the array from the identitifer array we build earlier | |
kind: FieldType.FVar(macro:Array<flixel.util.FlxColor>, macro $a{palette_var}), | |
pos:Context.currentPos() | |
}); | |
// Aaaand, we're done! o/ | |
return fields; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment