Color3:ToHex() can error on some platforms, and not on others (and on those it errors on, is incorrect)

I have a UI that shows a bunch of colors and runs color:ToHex() on them for testing purposes. This works fine on my machine, a Windows 11 PC with an Intel i7-9700K CPU, but for a lot of our players it gives the following error:

Unable to convert color to valid hex code

In the following place I copy the colors that we were showing and run ToHex() on all of them and report if they fail:

The colors are as follows:

Color3.fromHSV(0, 0, 0),
Color3.fromHSV(0, 0.6, 1),
Color3.fromHSV(40 / 360, 0.6, 1),
Color3.fromHSV(60 / 360, 0.6, 1),
Color3.fromHSV(120 / 360, 0.6, 1),
Color3.fromHSV(200 / 360, 0.6, 1),
Color3.fromHSV(240 / 360, 0.6, 1),
Color3.fromHSV(280 / 360, 0.6, 1),
Color3.fromHSV(300 / 360, 0.6, 1),
Color3.new(1, 1, 1),

My Samsung Galaxy S20 reports that color 9 (Color3.fromHSV(300 / 360, 0.6, 1)) fails.

6 Likes

Thanks for the report! I filed a ticket in our internal database and we’ll follow up when we have an update for you.

I haven’t tested the provided code on an Android device, but I have a theory as to why it is failing.

If I run this code:

print(Color3.fromHSV(300 / 360, 0.6, 1))

I get a result of:

 1, 0.4, 1

when running on an x64 PC.

So the Color3 value’s first component is either exactly 1, or so close that the printed output is 1. If the conversion from HSV on other platforms (e.g. the S20) is slightly different due to small floating point differences and the first component is slightly above 1.0 then ToHex will fail. If you clamp the values of the color between 0 and 1 then I imagine this issue will go away.

Bumping this topic because it has become an issue on Macs using M-series chips as well. It happens just as described in the original post.


When I run this code I also get 1, 0.4, 1 as a result. When I try to convert that color to Hex format, I get the Unable to convert color to valid hex code error.

You are right. When I convert this color (Color3.fromHSV(300 / 360, 0.6, 1)) to HSV, I get 0.8333333134651184, 0.6000000834465027, 1.0000001192092896.

This looks like an issue with how fromHSV() creates colors since Color3.new(1, 0.4, 1):ToHex() doesn’t cause an problems.

1 Like