| Thread overview | ||||||
|---|---|---|---|---|---|---|
|
October 16, 2020 How can I convert Hexadecimal to RGB Color and vice-versa? | ||||
|---|---|---|---|---|
| ||||
How can I convert Hexadecimal to RGB Color and vice-versa? | ||||
October 16, 2020 Re: How can I convert Hexadecimal to RGB Color and vice-versa? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Marcone | 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?
Do you mean from a string like "#123456" to the values of red, green, 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));
if (s.front == '#') {
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
| |||
November 24, 2020 Re: How can I convert Hexadecimal to RGB Color and vice-versa? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | // Função hex2rgb()
uint hex2rgb(string hexcolor) nothrow {
try {
uint value;
hexcolor.stripLeft("#").formattedRead!"%x"(value);
return value;
} catch(Throwable){return 0;}
}
// Função rgb2hex()
string rgb2hex(uint rgbcolor) nothrow {
try {
return "#%s".format(toHex(cast(const(BigInt)) rgbcolor));
} catch(Throwable){return "";}
}
| |||
November 24, 2020 Re: How can I convert Hexadecimal to RGB Color and vice-versa? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Marcone | 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
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply