<div dir="ltr">I recently came across a place where I needed this conversion. This is what I did:<br><br>String fullName = color.toString();<br>String cssName = "#" + fullName.substring(2, fullName.length() - 2);<div><br></div><div>Is this what you want to avoid writing? (Ignoring that the javadox of toString() says "<span style="font-size:1em">The content and format of the returned string might vary between implementations.")</span></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sat, Dec 16, 2023 at 4:07 PM Eran Leshem <eran@leshem.life> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div class="msg1528617025002575734"><div lang="EN-US"><div class="m_1528617025002575734WordSection1"><p class="MsoNormal"><span style="font-size:11pt;color:rgb(31,73,125)">Thanks. <u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:11pt;color:rgb(31,73,125)"><u></u> <u></u></span></p><p class="MsoNormal"><span style="font-size:11pt;color:rgb(31,73,125)">The #rrggbbaa format isn’t documented on <a href="https://openjfx.io/javadoc/21/javafx.graphics/javafx/scene/doc-files/cssref.html#typecolor" target="_blank">https://openjfx.io/javadoc/21/javafx.graphics/javafx/scene/doc-files/cssref.html#typecolor</a> for some reason, so I missed it. Can I contribute a fix to the doc?<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:11pt;color:rgb(31,73,125)"><u></u> <u></u></span></p><p class="MsoNormal"><span style="font-size:11pt;color:rgb(31,73,125)">What’s the process for approving option 1?<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:11pt;color:rgb(31,73,125)"> <u></u><u></u></span></p><div><p class="MsoNormal"><span style="font-size:11pt;font-family:"Freestyle Script";color:rgb(31,73,125)">Eran<u></u><u></u></span></p></div><p class="MsoNormal"><span style="font-size:11pt;color:rgb(31,73,125)"><u></u> <u></u></span></p><div><div style="border-right:none;border-bottom:none;border-left:none;border-top:1pt solid rgb(181,196,223);padding:3pt 0cm 0cm"><p class="MsoNormal"><b><span style="font-family:Tahoma,"sans-serif"">From:</span></b><span style="font-family:Tahoma,"sans-serif""> openjfx-dev [mailto:<a href="mailto:openjfx-dev-retn@openjdk.org" target="_blank">openjfx-dev-retn@openjdk.org</a>] <b>On Behalf Of </b>Kevin Rushforth<br><b>Sent:</b> Wednesday, December 13, 2023 2:46 PM<br><b>To:</b> <a href="mailto:openjfx-dev@openjdk.org" target="_blank">openjfx-dev@openjdk.org</a><br><b>Subject:</b> Re: Converting a Color object to its string representation<u></u><u></u></span></p></div></div><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal" style="margin-bottom:12pt">Or the third option:<br><br>3. Do nothing<br><br>I would go for option 1 or 3. I do not recommend option 2.<br><br>I see some value in a minimal API (option 1), returning a fixed format, perhaps:<br><br>    if alpha == 1 { "#rrggbb" } else { "#rrggbbaa" }<br><br>-- Kevin<br><br><u></u><u></u></p><div><p class="MsoNormal">On 12/12/2023 4:04 PM, Eran Leshem wrote:<u></u><u></u></p></div><blockquote style="margin-top:5pt;margin-bottom:5pt"><p class="MsoNormal"><span style="font-size:11pt;color:rgb(31,73,125)">I can see two options:</span><u></u><u></u></p><p class="m_1528617025002575734MsoListParagraph"><u></u><span>1.<span style="font:7pt "Times New Roman"">       </span></span><u></u><span dir="LTR"></span><span style="font-size:11pt;color:rgb(31,73,125)">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.</span><u></u><u></u></p><p class="m_1528617025002575734MsoListParagraph"><u></u><span>2.<span style="font:7pt "Times New Roman"">       </span></span><u></u><span dir="LTR"></span><span style="font-size:11pt;color:rgb(31,73,125)">Complete – supporting all formats accepted by Color.web(), either via multiple methods or via an enum parameter</span><u></u><u></u></p><p class="MsoNormal"><span style="font-size:11pt;color:rgb(31,73,125)"> </span><u></u><u></u></p><div><p class="MsoNormal"><span style="font-size:11pt;font-family:"Freestyle Script";color:rgb(31,73,125)">Eran</span><u></u><u></u></p></div><p class="MsoNormal"><span style="font-size:11pt;color:rgb(31,73,125)"> </span><u></u><u></u></p><div><div style="border-right:none;border-bottom:none;border-left:none;border-top:1pt solid rgb(181,196,223);padding:3pt 0cm 0cm"><p class="MsoNormal"><b><span style="font-family:Tahoma,"sans-serif"">From:</span></b><span style="font-family:Tahoma,"sans-serif""> openjfx-dev [<a href="mailto:openjfx-dev-retn@openjdk.org" target="_blank">mailto:openjfx-dev-retn@openjdk.org</a>] <b>On Behalf Of </b>Andy Goryachev<br><b>Sent:</b> Tuesday, December 12, 2023 6:08 PM<br><b>To:</b> Scott Palmer; <a href="mailto:openjfx-dev@openjdk.org" target="_blank">openjfx-dev@openjdk.org</a><br><b>Subject:</b> Re: Converting a Color object to its string representation</span><u></u><u></u></p></div></div><p class="MsoNormal"> <u></u><u></u></p><p class="MsoNormal"><span style="font-size:11pt;font-family:"Iosevka Fixed SS16"">I also think that the platform will benefit from adding this symmetrical API.  </span><u></u><u></u></p><p class="MsoNormal"><span style="font-size:11pt;font-family:"Iosevka Fixed SS16""> </span><u></u><u></u></p><p class="MsoNormal"><span style="font-size:11pt;font-family:"Iosevka Fixed SS16"">It is less clear how that new API should deal with all the multiple variants of the web format (#rgb, #rrggbb, rgb, rgba, 0x*, ...).</span><u></u><u></u></p><p class="MsoNormal"><span style="font-size:11pt;font-family:"Iosevka Fixed SS16""> </span><u></u><u></u></p><p class="MsoNormal"><span style="font-size:11pt;font-family:"Iosevka Fixed SS16"">-andy</span><u></u><u></u></p><p class="MsoNormal"><span style="font-size:11pt;font-family:"Iosevka Fixed SS16""> </span><u></u><u></u></p><p class="MsoNormal"><span style="font-size:11pt;font-family:"Iosevka Fixed SS16""> </span><u></u><u></u></p><p class="MsoNormal"><span style="font-size:11pt;font-family:"Iosevka Fixed SS16""> </span><u></u><u></u></p><div id="m_1528617025002575734mail-editor-reference-message-container"><div><div style="border-right:none;border-bottom:none;border-left:none;border-top:1pt solid rgb(181,196,223);padding:3pt 0cm 0cm"><p class="MsoNormal" style="margin-bottom:12pt"><b><span style="font-size:12pt;color:black">From: </span></b><span style="font-size:12pt;color:black">openjfx-dev <a href="mailto:openjfx-dev-retn@openjdk.org" target="_blank"><openjfx-dev-retn@openjdk.org></a> on behalf of Scott Palmer <a href="mailto:swpalmer@gmail.com" target="_blank"><swpalmer@gmail.com></a><br><b>Date: </b>Monday, December 11, 2023 at 17:12<br><b>To: </b><a href="mailto:openjfx-dev@openjdk.org" target="_blank">openjfx-dev@openjdk.org</a> <a href="mailto:openjfx-dev@openjdk.org" target="_blank"><openjfx-dev@openjdk.org></a><br><b>Subject: </b>Re: Converting a Color object to its string representation</span><u></u><u></u></p></div><p class="MsoNormal"><span style="font-size:11pt">I agree. I was going to write pretty much this exact email, but you beat me to it.</span><u></u><u></u></p><div><p class="MsoNormal"><span style="font-size:11pt">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. </span><u></u><u></u></p></div><div><p class="MsoNormal"><span style="font-size:11pt">I make a base64 data URL from a dynamically generated style sheet to avoid writing temp CSS files to style the app. </span><u></u><u></u></p></div><div><p class="MsoNormal"><span style="font-size:11pt">I also needed to do this to work around the style sheet having higher priority than programmatically set colours as per my misunderstanding in <a href="https://bugs.openjdk.org/browse/JDK-8317434" target="_blank">https://bugs.openjdk.org/browse/JDK-8317434</a></span><u></u><u></u></p></div><div><p class="MsoNormal"><span style="font-size:11pt">So I see value in having Color implement something like this.</span><u></u><u></u></p><div><p class="MsoNormal"><span style="font-size:11pt"> </span><u></u><u></u></p><div><p class="MsoNormal"><span style="font-size:11pt">Scott</span><u></u><u></u></p></div><div><p class="MsoNormal" style="margin-bottom:12pt"><span style="font-size:11pt"> </span><u></u><u></u></p><blockquote style="margin-top:5pt;margin-bottom:5pt"><p class="MsoNormal" style="margin-bottom:12pt"><span style="font-size:11pt">On Dec 11, 2023, at 4:19 PM, Eran Leshem <a href="mailto:eran@leshem.life" target="_blank"><eran@leshem.life></a> wrote:</span><u></u><u></u></p></blockquote></div><blockquote style="margin-top:5pt;margin-bottom:5pt"><div><p class="MsoNormal" style="margin-bottom:12pt"><span style="font-size:11pt">Thank you for your responses.<br><br>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.<br><br>And given that Color already parses these string representations, I think it makes sense for it to provide the inverse conversion.<br><br>Eran<br><br>-----Original Message-----<br>From: openjfx-dev [<a href="mailto:openjfx-dev-retn@openjdk.org" target="_blank">mailto:openjfx-dev-retn@openjdk.org</a>] On Behalf Of John Hendrikx<br>Sent: Saturday, December 09, 2023 11:35 PM<br>To: <a href="mailto:openjfx-dev@openjdk.org" target="_blank">openjfx-dev@openjdk.org</a><br>Subject: Re: Converting a Color object to its string representation<br><br>I think this is too niche to have Color provide.<br><br>Just make a utility method for whatever format you desire, instead of <br>making Color responsible for half a dozen ways of formatting colors, and <br>then probably still missing some format that someone needs.<br><br>Ticket should be closed as won't fix.<br><br>--John<br><br>On 09/12/2023 22:06, Michael Strauß wrote:</span><u></u><u></u></p><blockquote style="margin-top:5pt;margin-bottom:5pt"><p class="MsoNormal"><span style="font-size:11pt">I obviously meant to write withPrefix("#"), not withDelimiter("#")...</span><u></u><u></u></p></blockquote><blockquote style="margin-top:5pt;margin-bottom:5pt"><p class="MsoNormal"><span style="font-size:11pt"> </span><u></u><u></u></p></blockquote><blockquote style="margin-top:5pt;margin-bottom:5pt"><p class="MsoNormal"><span style="font-size:11pt">On Sat, Dec 9, 2023 at 9:57 PM Michael Strauß <a href="mailto:michaelstrau2@gmail.com" target="_blank"><michaelstrau2@gmail.com></a> wrote:</span><u></u><u></u></p></blockquote><blockquote style="margin-top:5pt;margin-bottom:5pt"><blockquote style="margin-top:5pt;margin-bottom:5pt"><p class="MsoNormal"><span style="font-size:11pt">How would HexFormat work with Color, other than using an extremely</span><u></u><u></u></p></blockquote></blockquote><blockquote style="margin-top:5pt;margin-bottom:5pt"><blockquote style="margin-top:5pt;margin-bottom:5pt"><p class="MsoNormal"><span style="font-size:11pt">unwieldy syntax?</span><u></u><u></u></p></blockquote></blockquote><blockquote style="margin-top:5pt;margin-bottom:5pt"><blockquote style="margin-top:5pt;margin-bottom:5pt"><p class="MsoNormal"><span style="font-size:11pt"> </span><u></u><u></u></p></blockquote></blockquote><blockquote style="margin-top:5pt;margin-bottom:5pt"><blockquote style="margin-top:5pt;margin-bottom:5pt"><p class="MsoNormal"><span style="font-size:11pt">    String formatted = HexFormat.of()</span><u></u><u></u></p></blockquote></blockquote><blockquote style="margin-top:5pt;margin-bottom:5pt"><blockquote style="margin-top:5pt;margin-bottom:5pt"><p class="MsoNormal"><span style="font-size:11pt">        .withDelimiter("#")</span><u></u><u></u></p></blockquote></blockquote><blockquote style="margin-top:5pt;margin-bottom:5pt"><blockquote style="margin-top:5pt;margin-bottom:5pt"><p class="MsoNormal"><span style="font-size:11pt">        .formatHex(new byte[] {</span><u></u><u></u></p></blockquote></blockquote><blockquote style="margin-top:5pt;margin-bottom:5pt"><blockquote style="margin-top:5pt;margin-bottom:5pt"><p class="MsoNormal"><span style="font-size:11pt">            (byte)(color.getRed() * 255.0f),</span><u></u><u></u></p></blockquote></blockquote><blockquote style="margin-top:5pt;margin-bottom:5pt"><blockquote style="margin-top:5pt;margin-bottom:5pt"><p class="MsoNormal"><span style="font-size:11pt">            (byte)(color.getGreen() * 255.0f),</span><u></u><u></u></p></blockquote></blockquote><blockquote style="margin-top:5pt;margin-bottom:5pt"><blockquote style="margin-top:5pt;margin-bottom:5pt"><p class="MsoNormal"><span style="font-size:11pt">            (byte)(color.getBlue() * 255.0f),</span><u></u><u></u></p></blockquote></blockquote><blockquote style="margin-top:5pt;margin-bottom:5pt"><blockquote style="margin-top:5pt;margin-bottom:5pt"><p class="MsoNormal"><span style="font-size:11pt">            (byte)(color.getOpacity() * 255.0f)</span><u></u><u></u></p></blockquote></blockquote><blockquote style="margin-top:5pt;margin-bottom:5pt"><blockquote style="margin-top:5pt;margin-bottom:5pt"><p class="MsoNormal"><span style="font-size:11pt">        });</span><u></u><u></u></p></blockquote></blockquote><blockquote style="margin-top:5pt;margin-bottom:5pt"><blockquote style="margin-top:5pt;margin-bottom:5pt"><p class="MsoNormal"><span style="font-size:11pt"> </span><u></u><u></u></p></blockquote></blockquote><blockquote style="margin-top:5pt;margin-bottom:5pt"><blockquote style="margin-top:5pt;margin-bottom:5pt"><p class="MsoNormal"><span style="font-size:11pt"> </span><u></u><u></u></p></blockquote></blockquote><blockquote style="margin-top:5pt;margin-bottom:5pt"><blockquote style="margin-top:5pt;margin-bottom:5pt"><p class="MsoNormal"><span style="font-size:11pt"> </span><u></u><u></u></p></blockquote></blockquote><blockquote style="margin-top:5pt;margin-bottom:5pt"><blockquote style="margin-top:5pt;margin-bottom:5pt"><p class="MsoNormal"><span style="font-size:11pt">On Sat, Dec 9, 2023 at 9:40 PM David Alayachew <a href="mailto:davidalayachew@gmail.com" target="_blank"><davidalayachew@gmail.com></a> wrote:</span><u></u><u></u></p></blockquote></blockquote><blockquote style="margin-top:5pt;margin-bottom:5pt"><blockquote style="margin-top:5pt;margin-bottom:5pt"><blockquote style="margin-top:5pt;margin-bottom:5pt"><p class="MsoNormal"><span style="font-size:11pt">Apologies - java.util.HexFormat</span><u></u><u></u></p></blockquote></blockquote></blockquote><blockquote style="margin-top:5pt;margin-bottom:5pt"><blockquote style="margin-top:5pt;margin-bottom:5pt"><blockquote style="margin-top:5pt;margin-bottom:5pt"><p class="MsoNormal"><span style="font-size:11pt"> </span><u></u><u></u></p></blockquote></blockquote></blockquote><blockquote style="margin-top:5pt;margin-bottom:5pt"><blockquote style="margin-top:5pt;margin-bottom:5pt"><blockquote style="margin-top:5pt;margin-bottom:5pt"><p class="MsoNormal"><span style="font-size:11pt">That's what I get for firing from the hip, then looking afterwards.</span><u></u><u></u></p></blockquote></blockquote></blockquote><p class="MsoNormal"><span style="font-size:11pt"> </span><u></u><u></u></p></div></blockquote></div></div></div></div></blockquote><p class="MsoNormal"><span style="font-size:12pt;font-family:"Times New Roman","serif""><u></u> <u></u></span></p></div></div></div></blockquote></div>