DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Getting Started With Snowflake Snowpark ML: A Step-by-Step Guide
  • Automating Cucumber Data Table to Java Object Mapping in Your Cucumber Tests
  • The Hidden Costs of Lombok in Enterprise Java Solutions
  • DataWeave: Play With Dates (Part 1)

Trending

  • Implementing Observability in Distributed Systems Using OpenTelemetry
  • Getting Started With Agentic Workflows in Java and Quarkus
  • 5 Common Security Pitfalls in Serverless Architectures
  • Every Cache Miss Is a Tiny Tax on Your Performance
  1. DZone
  2. Data Engineering
  3. Data
  4. How to Create java.awt.Color From String Representation

How to Create java.awt.Color From String Representation

Check out this post on how to create java.awt.Color from string representations using the Text-IO library. Click here to learn more.

By 
Serban Iordache user avatar
Serban Iordache
·
Jul. 27, 18 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
18.9K Views

Join the DZone community and get the full member experience.

Join For Free

When using the Text-IO library, you can configure the text terminals via properties files. Colors can be specified using standard HTML names (e.g. orange, chocolate, crimson), hex strings (e.g. #ee5533, #e53, 0xEE5533), rgb format strings (e.g. rgb(217,33,119), rgba(112,25%,50%,0.9)), or hsl format strings (e.g. hsl(240,90%,70%), hsla(270,0%,100%,0.7)).

How does Text-IO create colors from these string representations? Until recently, the implementation used the static method web(String colorString) provided by javafx.scene.Color. This method supports all the above mentioned formats and returns a javafx.scene.Color instance. Text-IO needs a java.awt.Color, but creating one from the returned javafx.scene.Color is straightforward.

However, I was not happy with this solution. It made my code depend on JavaFX, although Text-IO doesn’t actually use JavaFX. And, starting with JDK 11, JavaFX is no longer part of the JDK.

So, I decided to write a little library named AWT Color Factory for creating java.awt.Color  objects from string representations. The simplest way to do this is to copy the code of the javafx.scene.Color.web(String colorString) method and adapt it to create  java.awt.Colorinstances.

But, what about licensing? JavaFX is released under the GPL, so my library must use the same license. Text-IO is, instead, released under the Apache-2.0 license. Is it then allowed to use my library? Yes, it is. The license under which JavaFX iwas released is actually not GPL but the GPL 2 with Classpath exception. The same applies to my library. This is very important, because the Classpath exception clause gives you permission to include the AWT Color Factory library in your executable code, regardless of the license under which your software is distributed.

Example Usage

Color c1 = ColorFactory.valueOf("firebrick");
Color c2 = ColorFactory.valueOf("#aa38e0");
Color c3 = ColorFactory.valueOf("0x40A8CC");
Color c4 = ColorFactory.valueOf("rgba(112,36,228,0.9)");
Color c5 = ColorFactory.web("forestgreen", 0.7);
Color c6 = ColorFactory.web("hsl(270,90%,70%)", 0.8);


AWT Color Factory is available in JCenter and Maven Central.

Maven

<dependency>
    <groupId>org.beryx</groupId>
    <artifactId>awt-color-factory</artifactId>
    <version>1.0.0</version>
</dependency>


Gradle

compile 'org.beryx:awt-color-factory:1.0.0'


The library requires Java 7 or newer. The jar artifact is modularized under the name org.beryx.awt.color.

Starting with version 3.2.0, Text-IO uses the AWT Color Factory library and no longer depends on JavaFX.

Strings Data Types Library

Published at DZone with permission of Serban Iordache. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Getting Started With Snowflake Snowpark ML: A Step-by-Step Guide
  • Automating Cucumber Data Table to Java Object Mapping in Your Cucumber Tests
  • The Hidden Costs of Lombok in Enterprise Java Solutions
  • DataWeave: Play With Dates (Part 1)

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook