Posted By

adrianparr on 02/09/11


Tagged

css hex red colour color RGB as3 Blue Green middle average mix


Versions (?)

AS3 Average Colour Between Two Colours (My version)


 / Published in: ActionScript 3
 

  1. var red:uint = 0xFF0000;
  2. var green:uint = 0x00FF00;
  3. var average:uint = averageColour(red, green);
  4. trace("average: " + average);
  5. var averageCSS:String = "#" + average.toString(16).toUpperCase();
  6. trace("averageCSS: " + averageCSS);
  7.  
  8. function averageColour(colour1:uint, colour2:uint):uint
  9. {
  10. var averageRed:uint = (((colour1 >> 16) & 0xFF) + ((colour2 >> 16) & 0xFF)) / 2;
  11. var averageGreen:uint = (((colour1 >> 8) & 0xFF) + ((colour2 >> 8) & 0xFF)) / 2;
  12. var averageBlue:uint = ((colour1 & 0xFF) + (colour2 & 0xFF)) / 2;
  13. var hex:uint = averageRed << 16 | averageGreen << 8 | averageBlue;
  14. return hex;
  15. }
  16.  
  17. // OUTPUT
  18. // average: 8355584
  19. // averageCSS: #7F7F00

Report this snippet  

You need to login to post a comment.