Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create RGB-Value in PL/SQL?

I have a big table in my database where I need to update the internally called "ColorByte" field. This field is calculated from RGB values and primarly used by Excel-VBA Skripts but also in WinForms C# Applications. It represents one integer value which represents a specific color.

This works as following in VBA (no working code just for clarification):

r = 5
g = 50
b = 200

colorByte = RGB(r,g,b)

Read more about the RGB-Function here.

Now I got a complex calculation scheme for becomming better RGB-Values out of our company specific data, developed in VBA by a member from our research-team. I have to define this calcution on database to easy update my big table and correct the "colorByte" field. The exact calculations are unimportant because this stuff works but:

Is there an equivalent of VBA RGB(r, g, b) Function which I can use in PL/SQL to complete my function?

Or does someone know what this function internally does, so that I can redefine it in PL/SQL?

like image 399
Sebi Avatar asked Dec 05 '16 14:12

Sebi


2 Answers

I think RGB(r,g,b) simply does

RGB(r,g,b) = 256 * 256 * r + 256 * g + b

Perhaps it is the other way around (i.e. r + 256*g + 256*256*b) but it I am sure you can find this out by simple try-and-error. It should be no problem to rebuild this in PL/SQL.

like image 45
Wernfried Domscheit Avatar answered Sep 24 '22 18:09

Wernfried Domscheit


No, there is no Oracle built-in RGB() function, but the formula is quite simple

colorByte = red + (green * 256) + (blue * 256 * 256)

So the function might look like this

create or replace function rgb(
  p_red   in number,
  p_green in number,
  p_blue  in number
) return number
is
begin
  return p_red + (p_green * 256) + (p_blue * 256 * 256);
end;

Test case:

select rgb(5, 50, 200) as color_value
  from dual

COLOR_VALUE
-----------
   13120005

1 row selected.
like image 110
Nick Krasnov Avatar answered Sep 21 '22 18:09

Nick Krasnov