digitalmars.D.learn - How can I convert Hexadecimal to RGB Color and vice-versa?
- Marcone (1/1) Oct 16 2020 How can I convert Hexadecimal to RGB Color and vice-versa?
- =?UTF-8?Q?Ali_=c3=87ehreli?= (51/53) Oct 16 2020 Do you mean from a string like "#123456" to the values of red, green,
- Marcone (14/14) Nov 23 2020 // Função hex2rgb()
- Jacob Carlborg (57/58) Nov 24 2020 Not sure if this is what you're looking for:
How can I convert Hexadecimal to RGB Color and vice-versa?
Oct 16 2020
On 10/16/20 1:10 PM, Marcone wrote:How can I convert Hexadecimal to RGB Color and vice-versa?On 10/16/20 1:10 PM, Marcone wrote:How can I convert Hexadecimal to RGB Color and vice-versa?and blue? I am having more fun with D than usual. So, I wrote the following without understanding your requirements. :) I am sure this already exists in various libraries. import std.exception; import std.range; import std.format; import std.stdio; import std.traits; struct RGB { ubyte red; ubyte green; ubyte blue; this(const(char)[] s) { enforce(s.length < 8, format!`Invalid RGB string: "%s"`(s)); s.popFront(); } uint value; s.formattedRead!"%x"(value); this(value); } this(T)(T value) if (isIntegral!T) { enforce(value >= 0 && value < 0x100_0000, format!"Invalid RGB value: %s (0x%,*?x)"(value, 2, '_', value)); ubyte popLowByte() { ubyte b = value & 0xff; value >>= 8; return b; } this.blue = popLowByte(); this.green = popLowByte(); this.red = popLowByte(); assert(value == 0); } string asHex() { return format!"%02x%02x%02x"(red, green, blue); } } void main() { auto a = RGB("#a0a0a0"); writeln(a); auto b = RGB(0x10ff20); writeln(b); writeln(b.asHex); } Ali
Oct 16 2020
// Função hex2rgb() uint hex2rgb(string hexcolor) nothrow { try { uint value; return value; } catch(Throwable){return 0;} } // Função rgb2hex() string rgb2hex(uint rgbcolor) nothrow { try { } catch(Throwable){return "";} }
Nov 23 2020
On Friday, 16 October 2020 at 20:10:58 UTC, Marcone wrote:How can I convert Hexadecimal to RGB Color and vice-versa?Not sure if this is what you're looking for: struct Color { private uint hex; int red() out(result; result >= 0 && result <= 255) // assert that the result is within bounds { return (hex & 0xFF0000) >> 16; } void red(int value) in(value >= 0 && value <= 255) // assert that the value is within bounds { hex = (hex & 0x00FFFF) | (value << 16); } int green() out(result; result >= 0 && result <= 255) // assert that the result is within bounds { return (hex & 0x00FF00) >> 8; } void green(int value) in(value >= 0 && value <= 255) // assert that the value is within bounds { hex = (hex & 0xFF00FF) | (value << 8); } int blue() out(result; result >= 0 && result <= 255) // assert that the result is within bounds { return hex & 0x0000FF; } void blue(int value) in(value >= 0 && value <= 255) // assert that the value is within bounds { hex = (hex & 0xFFFF00) | value; } string toString() { return format!"Color(red: %s, green: %s, blue: %s)"(red, green, blue); } } void main() { Color color; color.red = 255; color.green = 143; color.blue = 89; writeln(color); } -- /Jacob Carlborg
Nov 24 2020