AS3 Average Colour Between Two Colours (My version)


/ Published in: ActionScript 3
Save to your folder(s)



Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.