# AS3 How to make a color darker

12 posts

 How can I make a color darker having its hex value? Should I remove a fixed value from each of the channels? Are there function to make hex subtractions? thanks! Just from inspection, it looks like the hex value changes at an increasing rate when going from white to black. could you not just put a black object with 0 alpha above it and then increase the alpha the make it get darker? Im creating gradient buttons but the users may select only the main color. I suppose that to have a color darker I should subtract 1 to each of the 3 channels. So FFFFFF should become FEFEFE, if one of the channel (byte) is 00 then I would not subract 1 from it. I simply should find how to transfrom an hex number to integer, subtract form it and then make it again Hex. You need to make use of ColorTransform [http://www.republicofcode.com/tutorials/flash/as3colortransform/](http://www.republicofcode.com/tutorials/flash/as3colortransform/) Color takes in a Number in the form 0xYYYYYY Convert it into a String, and create a function that decreases a two-value Hex by 1, making use of substring, then convert it back into a Number for ColorTransform However, like I said above, decreasing by 1 will give you a wider range of dark tints than light tints, or at least it will take longer to darken than lighten. This is usually done with bitwise operators to isolate each channel into a separate uint, then combine them again. To get just the blue channel, _and_ the last 8 bits of the color `var b:uint = color & 0x0000FF;` For green, use the middle 8 bits, and shift them right by 8 bits so you can treat it like a normal uint ranging from 0 to 0xFF `var g:uint = (color & 0x00FF00) >> 8;` And the same for red, shifting the first 8 bits 16 places to the right `var r:uint = (color & 0xFF0000) >> 16;` Do whatever operations you want to r, g and b (subtract 1, multiply by some fraction etc), then you can shift r and g back to the positions and add the channels back together again `var newColor:uint = (r << 16) + (g << 8) + b;` you don’t add colours together, you OR them together `var newColour:uint = (r << 16) | (g << 8) | b;` though you may also want to use `Math.max` and `Math.min` to prevent going over 255 for each channel or under 0 You _can_ add them :p Though OR is faster of course. Good call. > *Originally posted by **[Moonkey](/forums/4/topics/90211?page=1#posts-1989567):*** > > You _can_ add them :p > > Though OR is faster of course. Good call. not for speed; for safety, since adding can overflow into a higher colour while oring prevents it; also a reason for boundries with min/max. granted, in this situation, there should be no overflow from adding rather than oring If you are looking to specifically make a color darker, RGB is very unwieldy. You should work in HSL, where you can simply lower the lightness. I made a couple of classes for converting between RGB and HSL, so if you want me to, I’ll post them up. Edit: You can check them out here: [RGB](http://snipplr.com/view/34818/as3--rgb-class-for-rgb-to-hex/) [HSL](http://snipplr.com/view/34817/as3--hsl-to-rgb-class/) > not for speed; for safety, since adding can overflow into a higher colour while oring prevents it; Sure, but you have the wrong colour either way. > *Originally posted by **[Moonkey](/forums/4/topics/90211?page=1#posts-1989858):*** > > Sure, but you have the wrong colour either way. it’s just one of my various programming ticks, and min/max would prevent adding be much different from oring