Henrique Müller
Henrique Müller

Reputation: 157

Create a darker color

I'm developing an aplication that loads a dynamic color from a XML file. In some ocasions, I need the aplication to get a similar but darker color from the hexadecimal I have. Each value would go down 33 in hex (51 in decimal). Something like:

  1. 0xFFFFFF would become 0xCCCCCC
  2. 0x6600CC would become 0x330099

The hex values I have are strings. I just can't figure out how to solve it in a simple way.

Please, help!

And remember, it's AS2!

Upvotes: 2

Views: 166

Answers (1)

kapex
kapex

Reputation: 29959

You should search for one of the many colour libraries out there. If you want to do it yourself, you need some understanding of bitwise operators. I think reducing the colours by some percent instead of subtracting could give you a nicer result.

var color:int = parseInt(colorString);

// use the shift operator to get individual colour values
var red:int = (color>> 16) & 0xFF;
var green:int = (color>> 8) & 0xFF;
var blue:int = color & 0xFF;

// change colours by subtracting. Todo: make sure colours are between 0 and 255
/* 
red -= 0x33;
green -= 0x33;
blue -= 0x33; */

// make colours darker by 10%
red *= 0.9;
green *= 0.9;
blue *= 0.9;

// combine individual colours
color = (red << 16) | (green << 8) | blue;

Upvotes: 4

Related Questions