title: JavaFX: get percieved Brightness of a Color
body: <p>I got a coloured area in JavaFX and would like to display Text on it.
  The text colour should be white or black depending on the percieved brightness
  of the background. The following is the method given by <a href="https://docs.oracle.com/javafx/2/api/javafx/scene/paint/Color.html#getBrightness()" rel="nofollow noreferrer">JavaFX.Color#getBrightness</a>. But this method is not convincing as I need to choose white for a Brightness up to 0.9.</p>
  
  <pre><code>if (getColor().getBrightness() &lt; 0.9) {
     setStyle("-fx-text-fill: " + "white" + ";");
  } else {
      setStyle("-fx-text-fill: " + "black" + ";");
  }
  </code></pre>
  
  <p>Still, the result looks poor for many background colors: </p>
  
  <p><img src="https://i.stack.imgur.com/1L0fz.png" alt="enter image description here"></p>
  
  <p><img src="https://i.stack.imgur.com/Ny8zf.png" alt="enter image description here"></p>