Skip to content
Snippets Groups Projects
Commit d42871de authored by brinn's avatar brinn
Browse files

extend: color mixture calculation to non-pure colors

add: unit tests

SVN: 22418
parent 4046e618
No related branches found
No related tags found
No related merge requests found
...@@ -150,4 +150,11 @@ public class HSBColor ...@@ -150,4 +150,11 @@ public class HSBColor
{ {
return Color.getHSBColor(hue, saturation, brightness); return Color.getHSBColor(hue, saturation, brightness);
} }
@Override
public String toString()
{
return "HSBColor [hue=" + hue + ", saturation=" + saturation + ", brightness=" + brightness
+ "]";
}
} }
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
package ch.systemsx.cisd.common.color; package ch.systemsx.cisd.common.color;
/** /**
* A class for calculating a mixed color from a set of pure colors of different relative * A class for calculating a mixed color from a set of pure colors of different relative
* intensities. * intensities.
...@@ -27,7 +28,7 @@ package ch.systemsx.cisd.common.color; ...@@ -27,7 +28,7 @@ package ch.systemsx.cisd.common.color;
public class MixColors public class MixColors
{ {
private static float[] calcWeights(PureHSBColor[] colors, float[] intensities) private static float[] calcWeights(HSBColor[] colors, float[] intensities)
{ {
final float[] effectiveIntensities = new float[intensities.length]; final float[] effectiveIntensities = new float[intensities.length];
float sum = 0.0f; float sum = 0.0f;
...@@ -50,7 +51,7 @@ public class MixColors ...@@ -50,7 +51,7 @@ public class MixColors
* @param colors The colors to mix. * @param colors The colors to mix.
* @param intensities The intensities of each color. Has to be the same length as * @param intensities The intensities of each color. Has to be the same length as
* <var>colors</var>. * <var>colors</var>.
* @return The mixed color. * @return The mixed (pure) color.
*/ */
public static PureHSBColor calcMixedColor(PureHSBColor[] colors, float[] intensities) public static PureHSBColor calcMixedColor(PureHSBColor[] colors, float[] intensities)
{ {
...@@ -69,4 +70,31 @@ public class MixColors ...@@ -69,4 +70,31 @@ public class MixColors
return new PureHSBColor(hue, brightness); return new PureHSBColor(hue, brightness);
} }
/**
* Calculates a mixed color from given <var>colors</var>.
*
* @param colors The colors to mix.
* @param intensities The intensities of each color. Has to be the same length as
* <var>colors</var>.
* @return The mixed color.
*/
public static HSBColor calcMixedColor(HSBColor[] colors, float[] intensities)
{
assert colors.length == intensities.length;
final float[] weights = calcWeights(colors, intensities);
float hue = 0.0f;
float saturation = 0.0f;
float brightness = 0.0f;
for (int i = 0; i < weights.length; ++i)
{
hue += weights[i] * colors[i].getHue();
saturation += weights[i] * colors[i].getSaturation();
brightness = Math.max(brightness, intensities[i] * colors[i].getBrightness());
}
return new HSBColor(hue, saturation, brightness);
}
} }
...@@ -21,14 +21,14 @@ import java.awt.Color; ...@@ -21,14 +21,14 @@ import java.awt.Color;
/** /**
* A pure color object in the HSB (Hue, Saturation, Brightness) color space. * A pure color object in the HSB (Hue, Saturation, Brightness) color space.
* <p> * <p>
* A pure color is defined by having saturation = 1.0. * A pure color is defined by having saturation = 1.0.
* *
* @author Bernd Rinn * @author Bernd Rinn
*/ */
public class PureHSBColor extends HSBColor public class PureHSBColor extends HSBColor
{ {
/** /**
* Constructs a PureHSBColor from the given {@link java.awt.Color}. * Constructs a PureHSBColor from the given {@link java.awt.Color}.
* *
* @throw {@link IllegalArgumentException} if <var>color</var> is not pure. * @throw {@link IllegalArgumentException} if <var>color</var> is not pure.
*/ */
...@@ -53,4 +53,10 @@ public class PureHSBColor extends HSBColor ...@@ -53,4 +53,10 @@ public class PureHSBColor extends HSBColor
return true; return true;
} }
@Override
public String toString()
{
return "PureHSBColor [hue=" + getHueDegree() + ", brightness=" + getBrightness() + "]";
}
} }
/*
* Copyright 2011 ETH Zuerich, CISD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.systemsx.cisd.common.color;
import static org.testng.AssertJUnit.*;
import org.testng.annotations.Test;
/**
* Test cases fir {@link MixColors}.
*
* @author Bernd Rinn
*/
public class MixColorsTest
{
@Test
public void testMixTwoPureColorsFullIntensity()
{
PureHSBColor color = MixColors.calcMixedColor(new PureHSBColor[]
{ new PureHSBColor(0.0f, 1.0f), new PureHSBColor(0.5f, 1.0f) }, new float[]
{ 1f, 1f });
assertEquals(90.0f, color.getHueDegree());
assertEquals(1.0f, color.getBrightness());
}
@Test
public void testMixTwoPureColorsFullAndHalfIntensity()
{
PureHSBColor color = MixColors.calcMixedColor(new PureHSBColor[]
{ new PureHSBColor(0.0f, 1.0f), new PureHSBColor(0.5f, 1.0f) }, new float[]
{ 1f, 0.5f });
assertEquals(60.0f, color.getHueDegree());
assertEquals(1.0f, color.getBrightness());
}
@Test
public void testMixTwoPureColorsFullAndHalfBrightness()
{
PureHSBColor color = MixColors.calcMixedColor(new PureHSBColor[]
{ new PureHSBColor(0.0f, 1.0f), new PureHSBColor(0.5f, 0.5f) }, new float[]
{ 1f, 1f });
assertEquals(60.0f, color.getHueDegree());
assertEquals(1.0f, color.getBrightness());
}
@Test
public void testMixTwoPureColorsZeroAndFullIntensity()
{
PureHSBColor color = MixColors.calcMixedColor(new PureHSBColor[]
{ new PureHSBColor(0.0f, 1.0f), new PureHSBColor(2f / 3f, 1.0f) }, new float[]
{ 0f, 1f });
assertEquals(240.0f, color.getHueDegree());
assertEquals(1.0f, color.getBrightness());
}
@Test
public void testMixThreePureColorsFullQuarterQuarterIntensity()
{
PureHSBColor color =
MixColors.calcMixedColor(new PureHSBColor[]
{ new PureHSBColor(0.0f, 1.0f), new PureHSBColor(1f / 6f, 1.0f),
new PureHSBColor(2f / 3f, 1.0f) }, new float[]
{ 1f, 0.25f, 0.25f });
assertEquals(50.0f, color.getHueDegree(), 1e-5f);
assertEquals(1.0f, color.getBrightness());
}
@Test
public void testMixThreePureColorsFullQuarterHalfIntensityHalfBrightness()
{
PureHSBColor color =
MixColors.calcMixedColor(new PureHSBColor[]
{ new PureHSBColor(0.0f, 1.0f), new PureHSBColor(1f / 6f, 0.5f),
new PureHSBColor(2f / 3f, 1.0f) }, new float[]
{ 1f, 0.5f, 0.25f });
assertEquals(50.0f, color.getHueDegree(), 1e-5f);
assertEquals(1.0f, color.getBrightness());
}
@Test
public void testMixFourPureColorsFullQuarterHalfIntensityHalfBrightness()
{
PureHSBColor color =
MixColors.calcMixedColor(new PureHSBColor[]
{ new PureHSBColor(1f / 3f, 1.0f), new PureHSBColor(0f, 0.5f),
new PureHSBColor(2f / 3f, 0.5f), new PureHSBColor(15f / 18f, 0.25f) },
new float[]
{ 0.9f, 0.1f, 0.1f, 0.1f });
assertEquals(124.39f, color.getHueDegree(), 5e-4f);
assertEquals(0.9f, color.getBrightness());
}
@Test
public void testMixTwoColorsFullIntensityDifferentSaturation()
{
HSBColor color = MixColors.calcMixedColor(new HSBColor[]
{ new HSBColor(0.0f, 1.0f, 1.0f), new HSBColor(0.5f, 0.5f, 1.0f) }, new float[]
{ 1f, 1f });
assertEquals(90.0f, color.getHueDegree());
assertEquals(0.75f, color.getSaturation());
assertEquals(1.0f, color.getBrightness());
}
@Test
public void testMixFourColorsFullQuarterHalfIntensityHalfBrightness()
{
HSBColor color =
MixColors.calcMixedColor(new HSBColor[]
{ new HSBColor(1f / 3f, 1.0f), new HSBColor(0f, 0.5f, 0.5f),
new HSBColor(2f / 3f, 0.5f, 0.5f),
new HSBColor(15f / 18f, 0.25f, 0.25f) }, new float[]
{ 1f, 0.1f, 0.1f, 0.1f });
System.out.println(color);
assertEquals(124f, color.getHueDegree());
assertEquals(0.9389f, color.getSaturation(), 1e-4f);
assertEquals(1f, color.getBrightness());
}
}
/*
* Copyright 2011 ETH Zuerich, CISD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.systemsx.cisd.common.color;
import static org.testng.AssertJUnit.*;
import org.testng.annotations.Test;
/**
* Test cases for the {@link WavelengthColor} class.
*
* @author Bernd Rinn
*/
public class WavelengthColorTest
{
@Test
public void testCommonDyes()
{
PureHSBColor dapi = WavelengthColor.getHSBColorForWavelength(461);
assertEquals(214.8f, dapi.getHueDegree(), 0.05f);
assertEquals(1.0f, dapi.getBrightness());
PureHSBColor gfp = WavelengthColor.getHSBColorForWavelength(509);
assertEquals(123.1f, gfp.getHueDegree(), 0.05f);
assertEquals(1.0f, gfp.getBrightness());
PureHSBColor fitc = WavelengthColor.getHSBColorForWavelength(521);
assertEquals(110.6f, fitc.getHueDegree(), 0.05f);
assertEquals(1.0f, fitc.getBrightness());
PureHSBColor cy5 = WavelengthColor.getHSBColorForWavelength(660);
assertEquals(0.0f, cy5.getHueDegree());
assertEquals(1.0f, cy5.getBrightness());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment