1919 */
2020package org .cerberus .core .crud .entity ;
2121
22+ import java .awt .Color ;
2223import lombok .EqualsAndHashCode ;
2324import lombok .Getter ;
2425import lombok .Setter ;
@@ -44,6 +45,7 @@ public class Label {
4445 private String label ;
4546 private String type ;
4647 private String color ;
48+ private String fontColor ;
4749 private Integer parentLabelID ;
4850 private String requirementType ;
4951 private String requirementStatus ;
@@ -80,6 +82,7 @@ public JSONObject toJson() {
8082 labelJson .put ("label" , this .getLabel ());
8183 labelJson .put ("type" , this .getType ());
8284 labelJson .put ("color" , this .getColor ());
85+ labelJson .put ("fontColor" , this .guessFontColor ());
8386 labelJson .put ("parentLabelID" , this .getParentLabelID ());
8487 labelJson .put ("requirementType" , this .getRequirementType ());
8588 labelJson .put ("requirementStatus" , this .getRequirementStatus ());
@@ -106,6 +109,7 @@ public JSONObject toJsonV001() {
106109 labelJson .put ("label" , this .getLabel ());
107110 labelJson .put ("type" , this .getType ());
108111 labelJson .put ("color" , this .getColor ());
112+ labelJson .put ("fontColor" , this .guessFontColor ());
109113 labelJson .put ("parentLabelID" , this .getParentLabelID ());
110114 labelJson .put ("requirementType" , this .getRequirementType ());
111115 labelJson .put ("requirementStatus" , this .getRequirementStatus ());
@@ -129,9 +133,38 @@ public JSONObject toJsonGUI() {
129133 result .put ("label" , this .getLabel ());
130134 result .put ("type" , this .getType ());
131135 result .put ("color" , this .getColor ());
136+ result .put ("fontColor" , this .guessFontColor ());
132137 } catch (JSONException ex ) {
133138 LOG .error (ex .toString (), ex );
134139 }
135140 return result ;
136141 }
142+
143+ public String guessFontColor () {
144+ return (this .isColorDark (this .getColor ()) ? "white" : "black" );
145+ }
146+
147+ public boolean isColorDark (String hexaCodeColor ) {
148+
149+ try {
150+ // remove hash character from string
151+ String rawFontColor = hexaCodeColor .substring (1 , hexaCodeColor .length ());
152+
153+ // convert hex string to int
154+ int rgb = Integer .parseInt (rawFontColor , 16 );
155+
156+ Color c = new Color (rgb );
157+
158+ float [] hsb = Color .RGBtoHSB (c .getRed (), c .getGreen (), c .getBlue (), null );
159+
160+ float brightness = hsb [2 ];
161+
162+ LOG .debug ("is the Color Dark ? " + hexaCodeColor + " : " + (brightness < 0.5 ));
163+ return (brightness < 0.5 );
164+ } catch (Exception e ) {
165+ LOG .warn ("Could not guess is color " + hexaCodeColor + "is Dark." , e );
166+ }
167+ return true ;
168+ }
169+
137170}
0 commit comments