import jalview.datamodel.AnnotatedCollectionI;
import jalview.datamodel.SequenceCollectionI;
import jalview.datamodel.SequenceI;
+import jalview.util.ColorUtils;
import jalview.util.Comparison;
import jalview.util.MessageManager;
float bleachFactor = (11 - (conservationScore - '0')) / 10f;
/*
- * scale this by the percentage slider / 20
- */
- bleachFactor *= (inc / 20f);
-
- int red = currentColour.getRed();
- int green = currentColour.getGreen();
- int blue = currentColour.getBlue();
-
- /*
- * bleach colours towards white (255, 255, 255),
- * depending on the consensus score and the conservation slider value
- * scores of: 0 1 2 3 4 5 6 7 8 9
+ * scale this up by 0-5 (percentage slider / 20)
+ * as a result, scores of: 0 1 2 3 4 5 6 7 8 9
* fade to white at slider value: 18 20 22 25 29 33 40 50 67 100%
*/
- red += (255 - red) * bleachFactor;
- green += (255 - green) * bleachFactor;
- blue += (255 - blue) * bleachFactor;
+ bleachFactor *= (inc / 20f);
- if (red > 255 || green > 255 || blue > 255)
- {
- currentColour = Color.white;
- }
- else
- {
- currentColour = new Color(red, green, blue);
- }
- return currentColour;
+ return ColorUtils.bleachColour(currentColour, bleachFactor);
}
@Override
* (maxColour.getBlue() - minColour.getBlue());
return new Color(r / 255, g / 255, b / 255);
}
+
+ /**
+ * 'Fades' the given colour towards white by the specified proportion. A
+ * factor of 1 or more results in White, a factor of 0 leaves the colour
+ * unchanged, and a factor between 0 and 1 results in a proportionate change
+ * of RGB values towards (255, 255, 255).
+ * <p>
+ * A negative bleachFactor can be specified to darken the colour towards Black
+ * (0, 0, 0).
+ *
+ * @param colour
+ * @param bleachFactor
+ * @return
+ */
+ public static Color bleachColour(Color colour, float bleachFactor)
+ {
+ if (bleachFactor >= 1f)
+ {
+ return Color.WHITE;
+ }
+ if (bleachFactor <= -1f)
+ {
+ return Color.BLACK;
+ }
+ if (bleachFactor == 0f)
+ {
+ return colour;
+ }
+
+ int red = colour.getRed();
+ int green = colour.getGreen();
+ int blue = colour.getBlue();
+
+ if (bleachFactor > 0)
+ {
+ red += (255 - red) * bleachFactor;
+ green += (255 - green) * bleachFactor;
+ blue += (255 - blue) * bleachFactor;
+ return new Color(red, green, blue);
+ }
+ else
+ {
+ float factor = 1 + bleachFactor;
+ red *= factor;
+ green *= factor;
+ blue *= factor;
+ return new Color(red, green, blue);
+ }
+ }
}
import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertNull;
+import static org.testng.AssertJUnit.assertSame;
import java.awt.Color;
.getGraduatedColour(40f, 10f, minColour, 10f, maxColour);
assertEquals(minColour, col);
}
+
+ @Test(groups = { "Functional" })
+ public void testBleachColour()
+ {
+ Color colour = new Color(155, 105, 55);
+ assertSame(colour, ColorUtils.bleachColour(colour, 0));
+ assertEquals(Color.WHITE, ColorUtils.bleachColour(colour, 1));
+ assertEquals(Color.WHITE, ColorUtils.bleachColour(colour, 2));
+ assertEquals(new Color(175, 135, 95),
+ ColorUtils.bleachColour(colour, 0.2f));
+ assertEquals(new Color(225, 210, 195),
+ ColorUtils.bleachColour(colour, 0.7f));
+
+ /*
+ * and some 'negative fade'
+ */
+ assertEquals(Color.BLACK, ColorUtils.bleachColour(colour, -1));
+ assertEquals(Color.BLACK, ColorUtils.bleachColour(colour, -2));
+ assertEquals(new Color(124, 84, 44),
+ ColorUtils.bleachColour(colour, -0.2f));
+ assertEquals(new Color(46, 31, 16), // with rounding down
+ ColorUtils.bleachColour(colour, -0.7f));
+ }
}