How does hexadecimal color work?

Hexadecimal uses sixteen distinct symbols, in the case of css color the symbols 0–9 to represent values zero to nine (obviously), and A, B, C, D, E, F to represent values ten to fifteen. So, using one Hexadecimal character you can represent 16 values. With two Hexadecimal you can represent 256 (16*16) values. In RGB you have colours represented by … Read more

Convert RGBA to HEX

Since alpha value both attenuates the background color and the color value, something like this could do the trick: function rgba2rgb(RGB_background, RGBA_color) { var alpha = RGBA_color.a; return new Color( (1 – alpha) * RGB_background.r + alpha * RGBA_color.r, (1 – alpha) * RGB_background.g + alpha * RGBA_color.g, (1 – alpha) * RGB_background.b + alpha … Read more

Swift 5.4 hex to NSColor

you could try something like this: EDIT: included toHex(alpha:), from code I probably got from the net somewhere many years ago. EDIT3,4: included the case for #RRGGBBAA EDIT 5: stripping blank spaces in the hex string, to make NSColor (hex:” # 2196f380 “) work as well. extension NSColor { convenience init(hex: String) { let trimHex … Read more

How to convert hex number to bin in Swift?

You can use NSScanner() from the Foundation framework: let scanner = NSScanner(string: str) var result : UInt32 = 0 if scanner.scanHexInt(&result) { println(result) // 37331519 } Or the BSD library function strtoul() let num = strtoul(str, nil, 16) println(num) // 37331519 As of Swift 2 (Xcode 7), all integer types have an public init?(_ text: … Read more

Why is Java able to store 0xff000000 as an int?

Just an addition to erickson’s answer: As he said, signed integers are stored as two’s complements to their respective positive value on most computer architectures. That is, the whole 2^32 possible values are split up into two sets: one for positive values starting with a 0-bit and one for negative values starting with a 1. … Read more

Verify if String is hexadecimal

Horrible abuse of exceptions. Don’t ever do this! (It’s not me, it’s Josh Bloch’s Effective Java). Anyway, I suggest private static final Pattern HEXADECIMAL_PATTERN = Pattern.compile(“\\p{XDigit}+”); private boolean isHexadecimal(String input) { final Matcher matcher = HEXADECIMAL_PATTERN.matcher(input); return matcher.matches(); }