Advertisement
Guest User

GBA Color

a guest
Dec 9th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.63 KB | None | 0 0
  1. using EmblemMagic;
  2. using System;
  3. using System.Collections;
  4.  
  5. namespace GBA
  6. {
  7.     /// <summary>
  8.     /// Represents a 16-bit GBA Color, in A1B5G5R5 format
  9.     /// </summary>
  10.     public struct Color
  11.     {
  12.         public const ushort NO_ALPHA = 0x7FFF;
  13.         public const ushort ALPHA = 0x8000;
  14.  
  15.         const ushort BITS_R = 0x001F;
  16.         const ushort BITS_G = 0x03E0;
  17.         const ushort BITS_B = 0x7C00;
  18.  
  19.         /// <summary>
  20.         /// The 16-bit number that is the GBA.Color itself - this is the only field of this struct.
  21.         /// </summary>
  22.         public UInt16 Value { get; set; }
  23.  
  24.  
  25.  
  26.         public Color(Byte[] data)
  27.         {
  28.             Value = Util.BytesToUInt16(data, false);
  29.         }
  30.         public Color(UInt16 value)
  31.         {
  32.             Value = value;
  33.         }
  34.         public Color(UInt32 value)
  35.         {
  36.             Value = Get16bitColor(value, (value & 0xFF000000) != 0);
  37.         }
  38.         public Color(byte alpha, byte red, byte green, byte blue)
  39.         {
  40.             Value = Get16bitColor((alpha == 0) ? false : true, red, green, blue);
  41.         }
  42.  
  43.  
  44.  
  45.         public byte GetValueR()
  46.         {
  47.             return (byte)(Value & BITS_R);
  48.         }
  49.         public byte GetValueG()
  50.         {
  51.             return (byte)((Value & BITS_G) >> 5);
  52.         }
  53.         public byte GetValueB()
  54.         {
  55.             return (byte)((Value & BITS_B) >> 10);
  56.         }
  57.         public bool GetAlpha()
  58.         {
  59.             return ((Value & ALPHA) == ALPHA);
  60.         }
  61.  
  62.         public Color SetValueR(byte set)
  63.         {
  64.             if (set >= 32) throw new Exception("Cannot set GBA.Color channel to " + set);
  65.             return new Color((UInt16)((Value & (ALPHA | BITS_G | BITS_B)) | set));
  66.         }
  67.         public Color SetValueG(byte set)
  68.         {
  69.             if (set >= 32) throw new Exception("Cannot set GBA.Color channel to " + set);
  70.             return new Color((UInt16)((Value & (ALPHA | BITS_R | BITS_B)) | (set << 5)));
  71.         }
  72.         public Color SetValueB(byte set)
  73.         {
  74.             if (set >= 32) throw new Exception("Cannot set GBA.Color channel to " + set);
  75.             return new Color((UInt16)((Value & (ALPHA | BITS_R | BITS_G)) | (set << 10)));
  76.         }
  77.         public Color SetAlpha(bool set)
  78.         {
  79.             if (set) return new Color((UInt16)(Value | ALPHA));
  80.             else  return new Color((UInt16)(Value & NO_ALPHA));
  81.         }
  82.  
  83.         /// <summary>
  84.         /// This is the same as simply calling 'Value'
  85.         /// </summary>
  86.         public UInt16 To16bit()
  87.         {
  88.             return Value;
  89.         }
  90.         /// <summary>
  91.         /// Returns 'this' Color as a 32-bit AARRGGBB color.
  92.         /// </summary>
  93.         public UInt32 To32bit()
  94.         {
  95.             return Get32bitColor(Value);
  96.         }
  97.         /// <summary>
  98.         /// Returns 'this' Color as a 2-length byte array.
  99.         /// </summary>
  100.         public Byte[] ToBytes(bool littleEndian)
  101.         {
  102.             return Util.UInt16ToBytes(Value, littleEndian);
  103.         }
  104.  
  105.         /// <summary>
  106.         /// Returns the color defined by the given channels, in A1B5G5R5 16-bit format
  107.         /// </summary>
  108.         public static UInt16 Get16bitColor(bool alpha, byte red, byte green, byte blue)
  109.         {
  110.             red   = (byte)(red   >> 3);
  111.             green = (byte)(green >> 3);
  112.             blue  = (byte)(blue  >> 3);
  113.             return (UInt16)(((alpha ? 1 : 0) << 15) | (blue << 10) | (green << 5) | (red));
  114.         }
  115.         /// <summary>
  116.         /// Returns the given 32-bit Color as A1B5G5R5 16-bit format
  117.         /// </summary>
  118.         public static UInt16 Get16bitColor(UInt32 color, bool alpha = false)
  119.         {
  120.             byte a = (byte)((color & 0xFF000000) >> 0x18);
  121.             byte r = (byte)((color & 0x00FF0000) >> 0x10);
  122.             byte g = (byte)((color & 0x0000FF00) >> 0x08);
  123.             byte b = (byte) (color & 0x000000FF);
  124.             return Get16bitColor((a == 0) ? false : true, r, g, b);
  125.         }
  126.         /// <summary>
  127.         /// Returns the given 16bit color as AARRGGBB 32bit
  128.         /// </summary>
  129.         public static UInt32 Get32bitColor(UInt16 color)
  130.         {
  131.             UInt32 result = (color >= ALPHA) ? 0x0 : 0xFF000000;
  132.             result |= (uint)((color & BITS_B) >>  7);
  133.             result |= (uint)((color & BITS_G) <<  6);
  134.             result |= (uint)((color & BITS_R) << 19);
  135.             return result;
  136.         }
  137.         /// <summary>
  138.         /// Returns the index of the color nearest to the one given (possibly -1)
  139.         /// </summary>
  140.         public static int GetNearest(Palette palette, Color color)
  141.         {
  142.             byte R = color.GetValueR();
  143.             byte G = color.GetValueG();
  144.             byte B = color.GetValueB();
  145.             int r;
  146.             int g;
  147.             int b;
  148.             int sum;
  149.             int min = 65536;
  150.             int result = -1;
  151.             for (int i = 0; i < palette.Count; i++)
  152.             {
  153.                 r = Math.Abs(R - palette[i].GetValueR());
  154.                 g = Math.Abs(G - palette[i].GetValueG());
  155.                 b = Math.Abs(B - palette[i].GetValueB());
  156.                 sum = r + g + b;
  157.                 if (sum < min)
  158.                 {
  159.                     min = sum;
  160.                     result = i;
  161.                 }
  162.             }
  163.             return result;
  164.         }
  165.  
  166.  
  167.  
  168.         override public string ToString()
  169.         {
  170.             return "GBA.Color: "
  171.                 + Util.UInt16ToHex(Value) + " | "
  172.                 + "R: " + GetValueR() + ", "
  173.                 + "G: " + GetValueG() + ", "
  174.                 + "B: " + GetValueB() + ", "
  175.                 + "A: " + (Value >= ALPHA);
  176.         }
  177.         override public bool Equals(object other)
  178.         {
  179.             if (!(other is Color)) return false;
  180.             Color color = (Color)other;
  181.             return (Value == color.Value);
  182.         }
  183.         override public int GetHashCode()
  184.         {
  185.             return Value.GetHashCode();
  186.         }
  187.        
  188.         public static bool operator ==(Color left, Color right)
  189.         {
  190.             return (left.Value == right.Value);
  191.         }
  192.         public static bool operator !=(Color left, Color right)
  193.         {
  194.             return (left.Value != right.Value);
  195.         }
  196.  
  197.         public static explicit operator System.Drawing.Color(GBA.Color color)
  198.         {
  199.             return System.Drawing.Color.FromArgb((int)color.To32bit());
  200.         }
  201.         public static explicit operator GBA.Color(System.Drawing.Color color)
  202.         {
  203.             return new Color(Get16bitColor((color.A != 0xFF), color.R, color.G, color.B));
  204.         }
  205.     }
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement