Advertisement
Guest User

HaxeFlixel Palette / Color Scheme Macro

a guest
Nov 28th, 2017
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haxe 4.67 KB | None | 0 0
  1. ////////////////////////////////////////////////////
  2. ////////////////////////////////////////////////////
  3. // created by @Laguna_999
  4. // to be used with *.scss or *.gpl color scheme files
  5. ////////////////////////////////////////////////////
  6. // Example
  7. ////////////////////////////////////////////////////
  8. // @:build(PaletteLoader.LoadPalette("assets/data/palettes/legendary.scss"))
  9. // class Palette
  10. // {
  11. // }
  12. ////////////////////////////////////////////////////
  13. ////////////////////////////////////////////////////
  14.  
  15.  
  16.  
  17. package;
  18. import haxe.macro.Context;
  19. import haxe.macro.Expr.Access;
  20. import haxe.macro.Expr.Field;
  21. import haxe.macro.Expr.FieldType;
  22. import sys.io.File;
  23.  
  24.  
  25. /**
  26.  * ...
  27.  * @author
  28.  */
  29. class PaletteLoader
  30. {
  31.    
  32.     private static function parseFile_gpl (fileName : String) : Map<String, Int>
  33.     {
  34.         var m : Map<String, Int> = new Map<String, Int>();
  35.        
  36.         var f : String = File.getContent(fileName);
  37.         var lines : Array<String> = f.split("\n");
  38.        
  39.         for (i in 4 ... lines.length)
  40.         {
  41.             var l : String = StringTools.ltrim(lines[i]);
  42.            
  43.             var ss : Array<String>  = l.split(" ");        
  44.            
  45.             var r: Int = Std.parseInt(StringTools.trim(ss[0]));
  46.             var g: Int = Std.parseInt(StringTools.trim(ss[1]));
  47.             var b: Int = Std.parseInt(StringTools.trim(ss[2]));
  48.             var colorName : String = "";
  49.            
  50.             var arr : Array<String> = [];
  51.             for (j in 3 ... ss.length)
  52.             {
  53.                 arr.push(StringTools.trim(ss[j]));
  54.             }
  55.             if (arr[0] == "primary")
  56.             {
  57.                
  58.                 var idx : Int = Std.parseInt(arr[1]);
  59.                 if (idx == 0) idx = 3; else if (idx < 3) idx = 6 - idx; else idx = 5 - idx;
  60.                 colorName = "color" + Std.string(idx);
  61.             }
  62.             else if (arr[0] == "secondary-1")
  63.             {
  64.                 var idx : Int = Std.parseInt(arr[1]);
  65.                 if (idx == 0) idx = 3; else if (idx < 3) idx = 6 - idx; else idx = 5 - idx;
  66.                 colorName = "secondary1_" + Std.string(idx);
  67.             }
  68.             else if (arr[0] == "secondary-2")
  69.             {
  70.                 var idx : Int = Std.parseInt(arr[1]);
  71.                 if (idx == 0) idx = 3; else if (idx < 3) idx = 6 - idx; else idx = 5 - idx;
  72.                 colorName = "secondary2_" + Std.string(idx);
  73.             }
  74.             else if (arr[0] == "complement")
  75.             {
  76.                 var idx : Int = Std.parseInt(arr[1]);
  77.                 if (idx == 0) idx = 3; else if (idx < 3) idx = 6 - idx; else idx = 5 - idx;
  78.                 colorName = "complement" + Std.string(idx);
  79.             }
  80.            
  81.             trace(l + "       " + colorName + " " + r + " " + g + " " + b);
  82.             var ic : Int =  (b)  + (g << 8) + (r << 8 << 8);
  83.             m[colorName] = ic;
  84.         }
  85.         return m;
  86.     }
  87.    
  88.    
  89.     private static function parseFile_scss (fileName : String) : Map<String, Int>
  90.     {
  91.         var m : Map<String, Int> = new Map<String, Int>();
  92.        
  93.         var f : String = File.getContent(fileName);
  94.         var lines : Array<String> = f.split("\n");
  95.        
  96.         for (l in lines)
  97.         {
  98.             if (l.indexOf(": rgba(") == -1) continue;
  99.             var s1 : Array<String>  = l.split(": rgba(");          
  100.             if (s1.length != 2) continue;
  101.             var colorName : String = s1[0].substr(1);
  102.            
  103.             var s2 : Array<String> = s1[1].substring(0, s1[1].length - 2).split(",");
  104.             if (s2.length != 4) continue;
  105.             var r: Int = Std.parseInt(s2[0]);
  106.             var g: Int = Std.parseInt(s2[1]);
  107.             var b: Int = Std.parseInt(s2[2]);
  108.             trace(colorName + " " + r + " " + g + " " + b);
  109.             var ic : Int =  (b)  + (g << 8) + (r << 8 << 8);
  110.             m[colorName] = ic;
  111.         }
  112.         return m;
  113.     }
  114.    
  115.     private static function MapToTypeArray(m : Map<String,Int> ) : Array < Field >
  116.     {
  117.         var fields : Array<Field> = new Array<Field>();
  118.         for (k in m.keys())
  119.         {
  120.             var pos = Context.currentPos();
  121.             // create a new field
  122.             var propertyField : Field =
  123.             {
  124.               name : k,
  125.               access : [Access.APublic, Access.AStatic],
  126.               kind: FieldType.FProp("default", "null", macro:Int,macro $v{m[k]}),
  127.               pos: pos,
  128.             };
  129.             fields.push(propertyField);
  130.         }
  131.         return fields;
  132.     }
  133.    
  134.  
  135.     public static macro function LoadPalette(fileName:String, pname: String = "") : Array<Field>
  136.     {
  137.         // get the currently available fields
  138.         var fields = Context.getBuildFields();
  139.    
  140.         var m : Map<String, Int> = new Map<String,Int>();
  141.        
  142.         if (StringTools.endsWith(fileName, "scss"))
  143.             m = parseFile_scss(fileName);
  144.         else if (StringTools.endsWith(fileName, "gpl"))
  145.         {
  146.             m = parseFile_gpl(fileName);
  147.             if (!m.exists("color4"))
  148.             {
  149.                 m["color4"] = m["color1_shade3"];
  150.             }
  151.             if (!m.exists("color5"))
  152.             {
  153.                 m["color5"] = m["color1_shade4"];
  154.             }
  155.         }
  156.        
  157.         for (k in m.keys())
  158.         {
  159.        
  160.             var pos = Context.currentPos();
  161.             // create a new field
  162.             var propertyField : Field =
  163.             {
  164.               name : k,
  165.               access : [Access.APublic, Access.AStatic],
  166.               kind: FieldType.FProp("default", "null", macro:Int,macro $v{m[k]}),
  167.               pos: pos,
  168.             };
  169.             fields.push(propertyField);
  170.        
  171.         }
  172.         return fields;
  173.     }
  174.    
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement