Search code examples
colorshexrgbunreal-engine4unreal-blueprint

UE4: Conver String HEX Color to RGB or RBGA in Blueprint


How to convert HEX Color String to RGB or RBGA in Unreal Engine's Blueprint. For example: #f0f8ff to 240/248/255 RGB vector.

Thanks a lot.


Solution

  • Since the Blueprint API does not support Hex to RGB or RGB to Hex, but the regular FColor struct does, I'd suggest, you write yourself a wrapper for it in a UBlueprintFunctionLibrary.

    I won't go into detail how to create one of these libraries, since you can find an easy tutorial in the Unreal Wiki. However, I may give you the code that will hopefully work:

    part of the header:

    /** Converts hex string to color. Supports formats RGB, RRGGBB, RRGGBBAA, RGB, #RRGGBB, #RRGGBBAA */
    UFUNCTION(BlueprintCallable, Category="YourFunctionLibrary")
    static FColor HexToColor(FString HexString);
    
    /** Converts color to hex string */
    UFUNCTION(BlueprintCallable, Category="YourFunctionLibrary")
    static FString ColorToHex(FColor Color);
    

    part of the compilation unit:

    FColor YourFunctionLibrary::HexToColor(FString HexString)
    {
        return FColor::FromHex(HexString);
    }
    
    FString YourFunctionLibrary::ColorToHex(FColor Color)
    {
        return Color.ToHex();
    }
    

    Haven't tried it yet, but hope it works!