0

Can you check how similar a code is to another colour, If e.g. 0xFFFFFF is similar to 0xFBFBFB then change colour.

im not sure how the colour code works, so if you know a tip on the code, can you please share

Adam Edney
  • 300
  • 1
  • 2
  • 11
  • similar how? and on the matter of color codes it's pretty simple 0xFF|FF|FF this is white. the first two numbers (16 and 16, because this is HEX) are red with a value of 255. then comes green and then blue. with bitshifting you could seperate the color values and then test if another color is in its range, i guess. –  May 07 '14 at 17:01
  • Maybe something like this might help: http://stackoverflow.com/questions/5392061/algorithm-to-check-similarity-of-colors-based-on-rgb-values-or-maybe-hsv Anyways it's a nice topic, but on the other hand @DodgerThud is right - 'similarity' is a strange definition here and I don't know how this can be resolved.. – Andrey Popov May 07 '14 at 17:23

1 Answers1

3

A simple method to determinate proximity between colors, is to do so for each color component. In AS3, a color is a number containing 3 components : red, green and blue (There's sometimes a fourth component for transparency - alpha)

Each component is contained in 1 byte, for a value of 0 to 255. (0x00 to 0xFF) Here is a way to get each color component from the color itself :

var red:int = (color & 0xFF0000) >>> 16;
var green:int = (color & 0xFF00) >>> 8;
var blue:int = color & 0xFF;

Once you have the color components of each color, you can compare them 2 by 2, and combine the results:

var red_proximity = Math.abs(red1 - red2);
var green_proximity = Math.abs(green1 - green2);
var blue_proximity = Math.abs(blue1 - blue2);

var color_proximity = red_proximity + green_proximity + blue_proximity;

The result will be a number between 0 and 765; 0 meaning the color are exactly the same, and 765 meaning that they are completely differents. (Note that I am using Math.abs() to ensure that the proximity values are always positive.)

The last step now is to choose a threshold value to determine if the colors are "too close". The chosen value is usually arbitrary, so test a few and take your pick. For the example, I'm using the value '84'.

if (color_proximity < 84) {
  // color is too close : change it
} else {
  // color is okay : do nothing ?
}

This is only one method, and if you have to play a lot with colors, search a bit on the web for other algorithm; It's always usefull.

Aralicia
  • 875
  • 5
  • 15
  • greets for the answer and the effort! :) It's a great one, even that it's just to be used programmatically (to check how close one color is to another). So if someone just needs to check how 'close' two colors are, in meaning of values, this is the right way! I would still like to know about color palettes and sections of color wheel. But again, +1 for you! :) – Andrey Popov May 07 '14 at 18:08
  • This a great, the amount of effort you done here is fantastic. I got my code up and running, im using the value of 300 for color_proximity so the colour will be quite different, So thanks for all you done it been really helpful and you told me why it works:), one question what does >>> means, which you have done here ((color & 0xFF0000) >>> 16) – Adam Edney May 07 '14 at 19:12
  • @AdamEdney He's using bitshifting and bitmasking. check this link: [click](http://en.wikipedia.org/wiki/Bitwise_operation) or this link for more as3 specific examples [click](http://www.bobbyberberyan.com/2010/08/bitwise-operations-in-actionscript-3/) –  May 07 '14 at 19:31
  • @DodgerThud Thanks for the link, great help :) – Adam Edney May 07 '14 at 20:02