Friday, January 24, 2014

Color Refactor progress

Our game works by linking different pixel values to different tiles values. Among other things.
package JM_LIB.graphics.color.constants 
{
 import flash.utils.Dictionary;
 import JM_LIB.graphics.color.constants.colorConstHelper.ColorConstHelper;
 import JM_LIB.graphics.color.UintRGBandBackConverter;
 
 /**
  * ...
  * 
  * Working out the patterns for what bit patterns will create what colors:
  * 
  * 3 bits.
  RGB
  FFF == white
  000 == black

  4 bits:
  RGB D == dark RGB pure-hue color.
  000 D == dark grey
  FFF D == light grey
  000 0 == black
  FFF 0 == white

  5 bits:---------------------------------------------------------------
  D = make it dark if bit is set.
  T = shift to tertiary if bit is set.
  RGB D S

  RGB D 0 == dark RGB pure-hue color.
  000 D 0 == dark grey
  FFF D 0 == light grey
  000 0 0 == black
  FFF 0 0 == white

  RGB D T == dark tertiary.

  000 D T == dark dark grey
  FFF D T == light light grey
  000 0 T == DARK middle-grey
  FFF 0 T == LIGHT middle-grey

  [W]   [LG]   [DG]   [B]

  * @author 
  */
 public class PNGColorConstants 
 {
  
  public function PNGColorConstants() { };
  
  
  //                               __RR____
  public static const RED:uint = 0xFFFF0000;
  //                                          __RRgg__
    public static const ORANGE:uint = 0xFFFF8000;   //more red than green.
  //                                      __RRGG__
   public static const YELLOW:uint = 0xFFFFFF00;
  //                                        __rrGG__
    public static const LIME:uint = 0xFF80FF00; //more green than red.
  //                                 ____GG__
  public static const GREEN:uint = 0xFF00FF00;
  //                                        ____GGbb
    public static const TEAL:uint = 0xFF00FF80;  //more green than blue. Use "TEAL" instead of "MINT".
  //                                    ____GGBB
   public static const CYAN:uint = 0xFF00FFFF;
  //                                       ____ggBB
    public static const SKY:uint = 0xFF0080FF;   //more blue than green
  //                                ______BB
  public static const BLUE:uint = 0xFF0000FF;
  //                                          __rr__BB
    public static const INDIGO:uint = 0xFF8000FF;  //more blue than red //Used to be called PURPLE
  //                                       __RR__BB
   public static const MAGENTA:uint = 0xFFFF00FF;
  //                                           __RR__bb
    public static const FUCHSIA:uint = 0xFFFF0080; //more red than blue //used to call this one FUSCHIA
                                                  //Having a hard time with this one. Decided to call it FUCHSIA again.
     
                 
  //                                   __RR____
  public static const DARKRED:uint = 0xFF800000;
  //                                              __RRgg__
    public static const DARKORANGE:uint = 0xFF804000;   //more red than green.
  //                                          __RRGG__
   public static const DARKYELLOW:uint = 0xFF808000;
  //                                            __rrGG__
    public static const DARKLIME:uint = 0xFF408000; //Lime == more yellow than green. more GREEN than RED.
  //                                     ____GG__
  public static const DARKGREEN:uint = 0xFF008000;
  //                                            ____GGbb
    public static const DARKTEAL:uint = 0xFF008040;  //more green than blue. Use "TEAL" instead of "MINT".
  //                                        ____GGBB
   public static const DARKCYAN:uint = 0xFF008080;
  //                                           ____ggBB
    public static const DARKSKY:uint = 0xFF004080;   //more blue than green
  //                                    ______BB
  public static const DARKBLUE:uint = 0xFF000080;
  //                                              __rr__BB
    public static const DARKINDIGO:uint = 0xFF400080;  //more blue than red //Used to be called PURPLE
  //                                           __RR__BB
   public static const DARKMAGENTA:uint = 0xFF800080;
  //                                              __RR__bb
    public static const DARKFUCHSIA:uint = 0xFF800040; //more red than blue //used to call this one FUSCHIA
                                                  //Having a hard time with this one. Decided to call it FUCHSIA again.
                 
                 
                 
                 
  //-----------------------------------------------------//
    public static const WHITE     :uint = 0xFFFFFFFF;
    public static const BLACK     :uint = 0xFF000000;
    public static const LIGHTGREY :uint = 0xFFC0C0C0; //0xC0 == 192 (128 + 64)
    
    public static const INVISIBLEMIDDLEGREY      :uint = 0xFF808080; //0x80 == 128
    
    public static const DARKGREY  :uint = 0xFF404040; //0x40 == 64  (128 - 64)
    
    /** Light-Light Grey **/
    public static const LLGREY    :uint = 0xFFE0E0E0; //0xE0 == 224 (192 + 32)
    
    /** Dark-Dark Grey **/
    public static const DDGREY    :uint = 0xFF202020; //0x20 == 32  ( 64 - 32)  AKA (64/2)
    
    
    public static const DARKMIDDLEGREY :uint = 0xFF606060; //0x60 == 96  (128 - 32)
    public static const LIGHTMIDDLEGREY:uint = 0xFFA0A0A0; //0xA0 == 160 (128 + 32)
  //-----------------------------------------------------//
  
  private static var _workingVec:Vector.;
  private static function append(inHexColor:uint, inColorIndex:int, inColorKey:String, inColorName:String):void
  {
   var cHelp:ColorConstHelper = ColorConstHelper.make(inHexColor, inColorIndex, inColorKey, inColorName);
  }
  
  /** 3 Bits. RGB one for each color channel that makes up a pixel **/
  private static function make3BitColorKey():Vector.
  {
   var out:Vector. = new Vector.();
   _workingVec = out;
   //BLACK
   append(BLACK, 0, "K", "BLACK");
   
   //PRIMARY:
   append(RED  , 1, "R", "RED");
   append(GREEN, 2, "G", "GREEN");
   append(BLUE , 3, "B", "BLUE" );
   
   //WHITE:
   append(WHITE, 4, "W", "WHITE");
   
   //SECONDARIES:
   append(CYAN   , 5, "C", "CYAN" );
   append(MAGENTA, 6, "M", "MAGENTA" );
   append(YELLOW , 7, "Y", "YELLOW" );
   
   return out;
  }//make3BitColorKey
  
  /** 4Bits: RGB-D D for bit flag to make the color dark or not. **/
  private static function make4BitColorKey():Vector.
  {
   var out:Vector. = make3BitColorKey();
   _workingVec = out;
   
   //greys:
   append(LIGHTGREY, 8, "E", "LIGHTGREY");
   append(DARKGREY , 9, "e", "DARKGREY" );
   
   //dark versions of primaries:
   append(DARKRED  , 10, "r", "DARKRED");
   append(DARKGREEN, 11, "g", "DARKGREEN");
   append(DARKBLUE , 12, "b", "DARKBLUE");
   
   //dark versions of secondaries:
   append(DARKCYAN   , 13, "c", "DARKCYAN");
   append(DARKMAGENTA, 14, "m", "DARKMAGENTA");
   append(DARKYELLOW , 15, "y", "DARKYELLOW");
   
   return out;
  }//make4BitColorKey
  
  private static function make5BitColorKey():Vector.
  {
   var out:Vector. = make4BitColorKey();
   _workingVec = out;
   
   //greys:
   append(LLGREY,          16, "w", "LLGREY"); //W==White. w==Light-Light Grey.
   append(DDGREY,          17, "k", "DDGREY"); //K==Black. K==Dark-Dark Grey.
   append(LIGHTMIDDLEGREY, 18, "X", "LIGHTMIDDLEGREY");
   append(DARKMIDDLEGREY , 19, "x", "DARKMIDDLEGREY");
   
   //tertiary colors:
   append(ORANGE , 20, "O", "ORANGE"); //Capitol "OWE".
   append(LIME   , 21, "L", "LIME");
   append(TEAL   , 22, "T", "TEAL");
   append(SKY    , 23, "S", "SKY" );
   append(INDIGO , 24, "I", "INDIGO");
   append(FUCHSIA, 25, "F", "FUCHSIA");
   
   //DARK tertiary colors:
   append(DARKORANGE , 26, "o", "DARKORANGE"); //letter OWE. NOT number zero.
   append(DARKLIME   , 27, "l", "DARKLIME");  //letter "lowercase L" not "1".
   append(DARKTEAL   , 28, "t", "DARKTEAL");
   append(DARKSKY    , 29, "s", "DARKSKY" );
   append(DARKINDIGO , 30, "i", "DARKINDIGO");
   append(DARKFUCHSIA, 31, "f", "DARKFUCHSIA");
   
   return out;
  }//make5BitColorKey
  
  public static function getMasterColorKeyList():Vector.
  {
   var out:Vector. = make5BitColorKey();
   colorListIntegrityCheck( out );
   return out;
  }
  
  /** Uses dictionarys to make sure all the values are UNIQUE **/
  private static function colorListIntegrityCheck(inLST:Vector.
  {
   //Checks to make sure your data starts at zero and is sequential.
   var lowestIndex:int = int.MAX_VALUE;
   var hightestIndex:int = int.MIN_VALUE;
   var numItems:int = 0;
   
   /** Dictionary of hex values. EX: 0xFFFF0000 **/
   var hexDict   :Dictionary = new Dictionary();
   
   /** Dictioanry of index values "1","2",3" **/
   var dexDict   :Dictionary  = new Dictionary();
   
   /** Dictioanry of character key values. EX: "O","B","R" **/
   var keyDict   :Dictionary = new Dictionary();
   
   /** Dictioanry of name values: EX: ORANGE, BLUE, RED **/
   var namDict   :Dictionary = new Dictionary();
   
   //Check to make sure we are not using duplicate values anywhere in our color key:
   var cch:ColorConstHelper;
   for (var i:int = 0; i < inLST.length; i++)
   {
    cch = inLST[i];
    
    if (cch.colorIndex != i)
    {//colorIndex same as vector position?
     //We are imposing this constraint so that PNGColorMapNoAlpha can use the inLST for
     //easy lookup of data based on the color index value. 
     throw new Error("The color index should match up with it's index within the vector");
    }//colorIndex same as vector position?
    
    numItems++;
    if (cch.colorIndex > hightestIndex) { hightestIndex = cch.colorIndex; }
    if (cch.colorIndex < lowestIndex  ) { lowestIndex   = cch.colorIndex; }
    
    //hex color check:
    if (hexDict[ cch.hexColor ] == true ) { doError("duplicate hex colors"); }
    hexDict[ cch.hexColor ] = true;
    
    //index value checK:
    if (dexDict[ cch.colorIndex ] == true ) { doError("duplicate color index values"); }
    dexDict[ cch.colorIndex ] = true;
    
    //key name check:
    if (keyDict[ cch.colorKey ] == true ) { doError("duplicate color key values"); }
    keyDict[ cch.colorKey ] = true;
    
    //color name check:
    if (namDict[ cch.colorName ] == true ) { doError("duplicate color name values"); }
    namDict[ cch.colorName ] = true;
   }
   
   if (lowestIndex != 0)
   {
    var er:String = "";
    er += "the lowest index should always be zero! Else plugg";
    er += "ing this info into a vector becomes problematic.";
    throw new Error( er);
   }//low check.
   
   if (hightestIndex != (numItems - 1) )
   {
    var e2:String = "";
    e2 += "the highest index should be number of items minus 1.";
    e2 += "otherwise, we probably have HOLES in the ordering of our indicies.";
    e2 += "holes makes it problematic to pack constants into a vector for quick lookup.";
    throw new Error(e2);
   }//high check.
   
   CONFIG::debug { trace("Success!");}
  }//colorListIntegrityCheck
  
  private static function doError(inErrorMessage:String):void
  {
   throw new Error( inErrorMessage );
  }
  
 }//class
}//package

No comments:

Post a Comment