X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=test%2Fjalview%2Fschemes%2FFeatureColourTest.java;fp=test%2Fjalview%2Fschemes%2FFeatureColourTest.java;h=81357fa29b8379406a3b9311dc68471df233cb9d;hb=0b1c761dfaa8242f122cf868e8897a06ec6eb727;hp=0000000000000000000000000000000000000000;hpb=8b9c85b131fbaa1b9b29c489b661089c0ea404bd;p=jalview.git diff --git a/test/jalview/schemes/FeatureColourTest.java b/test/jalview/schemes/FeatureColourTest.java new file mode 100644 index 0000000..81357fa --- /dev/null +++ b/test/jalview/schemes/FeatureColourTest.java @@ -0,0 +1,115 @@ +package jalview.schemes; + +import static org.testng.AssertJUnit.assertEquals; +import static org.testng.AssertJUnit.assertFalse; +import static org.testng.AssertJUnit.assertTrue; + +import jalview.datamodel.SequenceFeature; + +import java.awt.Color; + +import org.testng.annotations.Test; + +public class FeatureColourTest +{ + @Test(groups = { "Functional" }) + public void testIsColored_simpleColour() + { + FeatureColour fc = new FeatureColour(Color.RED); + assertTrue(fc.isColored(new SequenceFeature())); + } + + @Test(groups = { "Functional" }) + public void testIsColored_colourByLabel() + { + FeatureColour fc = new FeatureColour(); + fc.setColourByLabel(true); + assertTrue(fc.isColored(new SequenceFeature())); + } + + @Test(groups = { "Functional" }) + public void testIsColored_aboveThreshold() + { + // graduated colour range from score 20 to 100 + FeatureColour fc = new FeatureColour(Color.WHITE, Color.BLACK, 20f, + 100f); + + // score 0 is adjusted to bottom of range + SequenceFeature sf = new SequenceFeature("type", "desc", 0, 20, 0f, + null); + assertTrue(fc.isColored(sf)); + assertEquals(Color.WHITE, fc.getColor(sf)); + + // score 120 is adjusted to top of range + sf.setScore(120f); + assertEquals(Color.BLACK, fc.getColor(sf)); + + // value below threshold is still rendered + // setting threshold has no effect yet... + fc.setThreshold(60f); + sf.setScore(36f); + assertTrue(fc.isColored(sf)); + assertEquals(new Color(204, 204, 204), fc.getColor(sf)); + + // now apply threshold: + fc.setAboveThreshold(true); + assertFalse(fc.isColored(sf)); + // colour is still returned though ?!? + assertEquals(new Color(204, 204, 204), fc.getColor(sf)); + + sf.setScore(84); // above threshold now + assertTrue(fc.isColored(sf)); + assertEquals(new Color(51, 51, 51), fc.getColor(sf)); + } + + @Test(groups = { "Functional" }) + public void testGetColor_simpleColour() + { + FeatureColour fc = new FeatureColour(Color.RED); + assertEquals(Color.RED, fc.getColor(new SequenceFeature())); + } + + @Test(groups = { "Functional" }) + public void testGetColor_colourByLabel() + { + FeatureColour fc = new FeatureColour(); + fc.setColourByLabel(true); + SequenceFeature sf = new SequenceFeature("type", "desc", 0, 20, 1f, + null); + Color expected = UserColourScheme.createColourFromName("desc"); + assertEquals(expected, fc.getColor(sf)); + } + + @Test(groups = { "Functional" }) + public void testGetColor_Graduated() + { + // graduated colour from score 0 to 100, gray(128, 128, 128) to red(255, 0, 0) + FeatureColour fc = new FeatureColour(Color.GRAY, Color.RED, 0f, 100f); + // feature score is 75 which is 3/4 of the way from GRAY to RED + SequenceFeature sf = new SequenceFeature("type", "desc", 0, 20, 75f, + null); + // the colour gradient is computed in float values from 0-1 (where 1 == 255) + float red = 128 / 255f + 3 / 4f * (255 - 128) / 255f; + float green = 128 / 255f + 3 / 4f * (0 - 128) / 255f; + float blue = 128 / 255f + 3 / 4f * (0 - 128) / 255f; + Color expected = new Color(red, green, blue); + assertEquals(expected, fc.getColor(sf)); + } + + @Test(groups = { "Functional" }) + public void testGetColor_belowThreshold() + { + // gradient from [50, 150] from WHITE(255, 255, 255) to BLACK(0, 0, 0) + FeatureColour fc = new FeatureColour(Color.WHITE, Color.BLACK, 50f, + 150f); + SequenceFeature sf = new SequenceFeature("type", "desc", 0, 20, 70f, + null); + fc.setThreshold(100f); // ignore for now + assertTrue(fc.isColored(sf)); + assertEquals(new Color(204, 204, 204), fc.getColor(sf)); + + fc.setAboveThreshold(true); // feature lies below threshold + assertFalse(fc.isColored(sf)); + assertEquals(new Color(204, 204, 204), fc.getColor(sf)); + } +}