Re: How can I convert Hexadecimal to RGB Color and vice-versa?

2020-11-24 Thread Jacob Carlborg via Digitalmars-d-learn

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 & 0xFF) >> 16;
}

void red(int value)
in(value >= 0 && value <= 255) // assert that the value is 
within bounds

{
hex = (hex & 0x00) | (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 & 0xFF;
}

void blue(int value)
in(value >= 0 && value <= 255) // assert that the value is 
within bounds

{
hex = (hex & 0x00) | 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


Re: How can I convert Hexadecimal to RGB Color and vice-versa?

2020-11-23 Thread Marcone via Digitalmars-d-learn

// 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 "";}
}


Re: How can I convert Hexadecimal to RGB Color and vice-versa?

2020-10-16 Thread Ali Çehreli via Digitalmars-d-learn

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_,
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