Hex code to RGB

I made a colour picking system for walls. I now want to expand that system to allow hex codes to be put in a text box and get converted. However it doesn’t seem like there is anything that can convert Hex to RGB in a blueprint. Is there something I am missing, or is there something either in unreal or external code that would allow to do this. Or am I stuck just using RGB input.

You can always write a custom conversion function.
If you’re using C++, there’s a FParse::HexNumber() function in “Misc/Parse.h” that you can use.
If you’re using blueprints, I’m afraid you can’t use a default parser there, but you can make your own.

I’ve made a crude version of what it might be like in BPs. Maybe there’s a more efficient way of doing it, but IDK. Here: BP Hex to RGB

Hope it makes sense.

1 Like

Thank you, I have not looked into C++ at all yet. just been using blueprints. The blueprint looks quite complex but I will see if i can figure it out.

1 Like

If you mean “converting hex to RGB” for eg: 0x203040 to decimals for RGB, then its not that complicated.

Simple function that converts pair of characters like “C5” do integer.
String HEX is local constant in function that must be “0123456789ABCDEF” ie all hex numbers as characters in string.

This is how to use it to get R,G,B values as int:

ps.
You can make enum {R,G,B} and instead of integer index use that enum there for better look.

3 Likes

I created a simple function based on that to convert hex to linear color. Hex To Linear Color posted by DarkAt26 | blueprintUE | PasteBin For Unreal Engine hopefully that is useful for anybody that is searching for something like that :smiley:

1 Like

This is quite new (because ChatGPT is new):

So now my advice would be to install C++ Create C++ project.
Ask chat GPT to make whole function for yoursaelf.
Copy paste code from chat to CPP
And compile.

Even for me (I know CPP) this is really fast way to make those small C++ functions.
Usually at worst it is matter of fixing singe bug in what chat made. But then you do not have those 20-30 weird functions that do very simple things, instead you have single (or two) files in C++ with single class.

Example of this (I did not check this function for bugs, chat does them often, so be sure to check it):

my prompt:
Create function for unreal 5, that converts Text variable (containing hex values of color RGBA) into unreal linear color. Expose Color as text and resulting linear color to blueprints. Make this function Pure. IT should have linear color input variable that represents “error color” and if translation of input text cannot be done to linear color, set result as that error color. This should be C++ function.

Chat first result:

“ColorConversionLibrary.h:”

#pragma once

#include "Kismet/BlueprintFunctionLibrary.h"
#include "ColorConversionLibrary.generated.h"

UCLASS()
class YOURPROJECT_API UColorConversionLibrary : public UBlueprintFunctionLibrary
{
    GENERATED_BODY()

public:
    UFUNCTION(BlueprintPure, Category = "Color Conversion")
    static FLinearColor HexToLinearColor(const FText& ColorText, const FLinearColor& ErrorColor);
};

“ColorConversionLibrary.cpp:”

#include "ColorConversionLibrary.h"

FLinearColor UColorConversionLibrary::HexToLinearColor(const FText& ColorText, const FLinearColor& ErrorColor)
{
    FString ColorString = ColorText.ToString();
    FLinearColor Result;

    if (ColorString.StartsWith("#"))
    {
        ColorString.RemoveAt(0); // Remove the '#' character

        // Convert the remaining string to FColor
        FColor ParsedColor;
        if (ParsedColor.InitFromString(ColorString))
        {
            Result = ParsedColor;
        }
        else
        {
            Result = ErrorColor;
        }
    }
    else
    {
        Result = ErrorColor;
    }

    return Result;
}

And bonus is you will get used to C++. With second bonus: all code in blueprints gets much more maintainable.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.