Converting a Color object to its string representation
Eran Leshem
eran at leshem.life
Sat Dec 23 23:38:07 UTC 2023
Yes, exactly what I would like every client to avoid writing.
Eran
From: Nir Lisker [mailto:nlisker at gmail.com]
Sent: Saturday, December 23, 2023 9:24 PM
To: Eran Leshem
Cc: Kevin Rushforth; openjfx-dev at openjdk.org
Subject: Re: Converting a Color object to its string representation
I recently came across a place where I needed this conversion. This is what I did:
String fullName = color.toString();
String cssName = "#" + fullName.substring(2, fullName.length() - 2);
Is this what you want to avoid writing? (Ignoring that the javadox of toString() says "The content and format of the returned string might vary between implementations.")
On Sat, Dec 16, 2023 at 4:07 PM Eran Leshem <eran at leshem.life> wrote:
Thanks.
The #rrggbbaa format isn’t documented on https://openjfx.io/javadoc/21/javafx.graphics/javafx/scene/doc-files/cssref.html#typecolor for some reason, so I missed it. Can I contribute a fix to the doc?
What’s the process for approving option 1?
Eran
From: openjfx-dev [mailto:openjfx-dev-retn at openjdk.org] On Behalf Of Kevin Rushforth
Sent: Wednesday, December 13, 2023 2:46 PM
To: openjfx-dev at openjdk.org
Subject: Re: Converting a Color object to its string representation
Or the third option:
3. Do nothing
I would go for option 1 or 3. I do not recommend option 2.
I see some value in a minimal API (option 1), returning a fixed format, perhaps:
if alpha == 1 { "#rrggbb" } else { "#rrggbbaa" }
-- Kevin
On 12/12/2023 4:04 PM, Eran Leshem wrote:
I can see two options:
1. Minimal, just in order to satisfy the style APIs need – supporting a single format. I would go with non-% rgba, since it covers all dimensions in the most straightforward way.
2. Complete – supporting all formats accepted by Color.web(), either via multiple methods or via an enum parameter
Eran
From: openjfx-dev [mailto:openjfx-dev-retn at openjdk.org] On Behalf Of Andy Goryachev
Sent: Tuesday, December 12, 2023 6:08 PM
To: Scott Palmer; openjfx-dev at openjdk.org
Subject: Re: Converting a Color object to its string representation
I also think that the platform will benefit from adding this symmetrical API.
It is less clear how that new API should deal with all the multiple variants of the web format (#rgb, #rrggbb, rgb, rgba, 0x*, ...).
-andy
From: openjfx-dev <mailto:openjfx-dev-retn at openjdk.org> <openjfx-dev-retn at openjdk.org> on behalf of Scott Palmer <mailto:swpalmer at gmail.com> <swpalmer at gmail.com>
Date: Monday, December 11, 2023 at 17:12
To: openjfx-dev at openjdk.org <mailto:openjfx-dev at openjdk.org> <openjfx-dev at openjdk.org>
Subject: Re: Converting a Color object to its string representation
I agree. I was going to write pretty much this exact email, but you beat me to it.
I was implementing some user-configurable colour customizations in an application and needed to do it with style sheets, along with something that reads colours along the lines of what the new platform preferences API does.
I make a base64 data URL from a dynamically generated style sheet to avoid writing temp CSS files to style the app.
I also needed to do this to work around the style sheet having higher priority than programmatically set colours as per my misunderstanding in https://bugs.openjdk.org/browse/JDK-8317434
So I see value in having Color implement something like this.
Scott
On Dec 11, 2023, at 4:19 PM, Eran Leshem <mailto:eran at leshem.life> <eran at leshem.life> wrote:
Thank you for your responses.
Given that the framework requires colors in string format in its style APIs, I think it should provide some way to convert colors to strings as expected by these APIs. Otherwise, clients are forced to implement this bridging logic on their own, due to a framework gap.
And given that Color already parses these string representations, I think it makes sense for it to provide the inverse conversion.
Eran
-----Original Message-----
From: openjfx-dev [mailto:openjfx-dev-retn at openjdk.org] On Behalf Of John Hendrikx
Sent: Saturday, December 09, 2023 11:35 PM
To: openjfx-dev at openjdk.org
Subject: Re: Converting a Color object to its string representation
I think this is too niche to have Color provide.
Just make a utility method for whatever format you desire, instead of
making Color responsible for half a dozen ways of formatting colors, and
then probably still missing some format that someone needs.
Ticket should be closed as won't fix.
--John
On 09/12/2023 22:06, Michael Strauß wrote:
I obviously meant to write withPrefix("#"), not withDelimiter("#")...
On Sat, Dec 9, 2023 at 9:57 PM Michael Strauß <mailto:michaelstrau2 at gmail.com> <michaelstrau2 at gmail.com> wrote:
How would HexFormat work with Color, other than using an extremely
unwieldy syntax?
String formatted = HexFormat.of()
.withDelimiter("#")
.formatHex(new byte[] {
(byte)(color.getRed() * 255.0f),
(byte)(color.getGreen() * 255.0f),
(byte)(color.getBlue() * 255.0f),
(byte)(color.getOpacity() * 255.0f)
});
On Sat, Dec 9, 2023 at 9:40 PM David Alayachew <mailto:davidalayachew at gmail.com> <davidalayachew at gmail.com> wrote:
Apologies - java.util.HexFormat
That's what I get for firing from the hip, then looking afterwards.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/openjfx-dev/attachments/20231224/ac0f63ba/attachment-0001.htm>
More information about the openjfx-dev
mailing list