#Convert Ultima Hue Colors to RGB

1 messages · Page 1 of 1 (latest)

mental storm
#

This script shows how to convert ultima online colours and hues code into standard RGB values.
Written by @stiff forum, but i posted it here because he is too lazy


def hue2rgb(hue):
    scale = 255 // 31
    r = ((hue & 0x7c00) >> 10) * scale
    g = ((hue & 0x03e0) >> 5) * scale
    b = (hue & 0x001f) * scale
    return (r, g, b)

hue = 1178
rgb = hue2rgb(hue)

Misc.SendMessage(f"Hue {hue} in RGB is {rgb}", hue)

The source used to make that function comes from the RE codebase:
https://github.com/RazorEnhanced/RazorEnhanced/blob/2ed41531488ffcf44474bf425db9ed90b28b939a/UltimaSDK/Ultima/Hues.cs#L161

        public static int HueToColorR(short hue)
        {
            return (((hue & 0x7c00) >> 10) * (255 / 31));
        }

        public static int HueToColorG(short hue)
        {
            return (((hue & 0x3e0) >> 5) * (255 / 31));
        }

        public static int HueToColorB(short hue)
        {
            return ((hue & 0x1f) * (255 / 31));
        }
stiff forum
#

Here is a PHP Version:

    $scale = intval(255 / 31);
    $r = (($hue & 0x7c00) >> 10) * $scale;
    $g = (($hue & 0x03e0) >> 5) * $scale;
    $b = ($hue & 0x001f) * $scale;
    return array($r, $g, $b);
}```
#

Applied like this over an image:

for ($x = 0; $x < $w; $x++) {
    for ($y = 0; $y < $h; $y++) {
        // Get the color of the pixel
        $colorIndex = imagecolorat($src, $x, $y);
        $color = imagecolorsforindex($src, $colorIndex);
        
        // Check if the pixel is grayscale
        if ($color['red'] == $color['green'] && $color['green'] == $color['blue']) {
            // The pixel is grayscale
            
            // Calculate the grayscale value
            $gray = $color['red'];  // Since R=G=B, we can just use one of them
            
            // Map the grayscale value to a hue
            $hueIndex = round($gray / 255 * (count($hues) - 1));
            
            // Get the hue
            $hue = $hues[$hueIndex];
            
            // Create a color in the destination image
            $colorIndex = imagecolorallocatealpha($dest, $hue[0], $hue[1], $hue[2], $color['alpha']);
        } else {
            // The pixel is not grayscale, use the original color
            $colorIndex = imagecolorallocatealpha($dest, $color['red'], $color['green'], $color['blue'], $color['alpha']);
        }
        
        // Set the color of the pixel in the destination image
        imagesetpixel($dest, $x, $y, $colorIndex);
    }
}```
mental storm
#

Haaaa, the GD libs, so many memories 🤣