Worst-case scenario: the UEd Goblin wipes the map and burns down your house.

Legacy:RGB To HLS Conversion

From Unreal Wiki, The Unreal Engine Documentation Site
Jump to: navigation, search

This is a function to convert RGB values to HLS. If you want to code colour changing lights in unreal and find that either you can't think in HLS or the light colour is supposed to represent some parameter that's easier coded with RGB you might find this code snippet useful.

To use this function just feed it a vector (R,G,B) with values from 0...1 for its components and it'll return (H,L,S) ready for you to assign to a light.

 function vector colourmap ( vector rgb)
 {
  local float min;
  local float max;
  local vector hls;
  local float r,g,b,h,l,s;
  // clamp em all to range 0...1;
  rgb.x= Fclamp(rgb.x,0,1);
  rgb.y= Fclamp(rgb.y,0,1);
  rgb.z= Fclamp(rgb.z,0,1);
 
  r=rgb.x; //sanity improving assignments!
  g=rgb.y;
  b=rgb.z;
 
  // find minimum and maximum
  max = Fmax(fmax(r,g),b) ;
  min = Fmin(Fmin(r,g),b);
 
  l = (max+min)/2; //lightness
 
  if (max==min)  //i.e it's pure grey
   {
     s = 0;
     h = 0;
   }
  else
   {
    if (l < 0.5)  s =(max-min)/(max+min);
    If (l >=0.5)  s =(max-min)/(2.0-max-min);
   }
 
  If (R == max)  h  = (G-B)/(max-min);
  If (G == max) h = 2.0 + (B-R)/(max-min);
  If (B == max)    h = 4.0 + (R-G)/(max-min);
 
  //this leaves h from 0...6 , l,s from 0..1 now scale to what unreal wants:
  // in this case 0..255   ,apart from saturation which seems to want 0..100 ish
  hls.x=(h/6)*255;
  hls.y=(l*255);
  hls.z=(s*127);
 
  return( hls);
 }

Be sure to...[edit]

DJPaul: This function expects an RGB value from 0..1 and outputs it in the range unreal expects. If you have a colour in RGB (or an RGB "Colour Picker") that you want to find the HLS equivalent of, do as follows:

  • For each R, G and B value:
    • cast them to floats
    • then divide by 255.0

Comments[edit]

Wormbo: The input and output formats of this function are kind of strange. I'd probably use something like this to work with this function:

function RGBToHLS(color InRGB, out byte Hue, out byte Luminance, out byte Saturation)
{
    local vector RGB, HLS;
 
    RGB.X = InRGB.R / 255.0;
    RGB.Y = InRGB.G / 255.0;
    RGB.Z = InRGB.B / 255.0;
    HLS = colourmap(RGB);
    Hue = HLS.X;
    Luminance = HLS.Y;
    Saturation = HLS.Z;
}

Related Topics[edit]