|
| 1 | +/* |
| 2 | + This file is part of the iText (R) project. |
| 3 | + Copyright (c) 1998-2019 iText Group NV |
| 4 | + Authors: iText Software. |
| 5 | +
|
| 6 | + This program is free software; you can redistribute it and/or modify |
| 7 | + it under the terms of the GNU Affero General Public License version 3 |
| 8 | + as published by the Free Software Foundation with the addition of the |
| 9 | + following permission added to Section 15 as permitted in Section 7(a): |
| 10 | + FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY |
| 11 | + ITEXT GROUP. ITEXT GROUP DISCLAIMS THE WARRANTY OF NON INFRINGEMENT |
| 12 | + OF THIRD PARTY RIGHTS |
| 13 | +
|
| 14 | + This program is distributed in the hope that it will be useful, but |
| 15 | + WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 16 | + or FITNESS FOR A PARTICULAR PURPOSE. |
| 17 | + See the GNU Affero General Public License for more details. |
| 18 | + You should have received a copy of the GNU Affero General Public License |
| 19 | + along with this program; if not, see http://www.gnu.org/licenses or write to |
| 20 | + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 21 | + Boston, MA, 02110-1301 USA, or download the license from the following URL: |
| 22 | + http://itextpdf.com/terms-of-use/ |
| 23 | +
|
| 24 | + The interactive user interfaces in modified source and object code versions |
| 25 | + of this program must display Appropriate Legal Notices, as required under |
| 26 | + Section 5 of the GNU Affero General Public License. |
| 27 | +
|
| 28 | + In accordance with Section 7(b) of the GNU Affero General Public License, |
| 29 | + a covered work must retain the producer line in every PDF that is created |
| 30 | + or manipulated using iText. |
| 31 | +
|
| 32 | + You can be released from the requirements of the license by purchasing |
| 33 | + a commercial license. Buying such a license is mandatory as soon as you |
| 34 | + develop commercial activities involving the iText software without |
| 35 | + disclosing the source code of your own applications. |
| 36 | + These activities include: offering paid services to customers as an ASP, |
| 37 | + serving PDFs on the fly in a web application, shipping iText with a closed |
| 38 | + source product. |
| 39 | +
|
| 40 | + For more information, please contact iText Software Corp. at this |
| 41 | + |
| 42 | + */ |
| 43 | +package com.itextpdf.kernel.colors; |
| 44 | + |
| 45 | +import com.itextpdf.io.LogMessageConstant; |
| 46 | +import com.itextpdf.test.ExtendedITextTest; |
| 47 | +import com.itextpdf.test.annotations.LogMessage; |
| 48 | +import com.itextpdf.test.annotations.LogMessages; |
| 49 | +import com.itextpdf.test.annotations.type.UnitTest; |
| 50 | +import org.junit.Assert; |
| 51 | +import org.junit.Test; |
| 52 | +import org.junit.experimental.categories.Category; |
| 53 | + |
| 54 | +import java.awt.Color; |
| 55 | + |
| 56 | +@Category(UnitTest.class) |
| 57 | +public class DeviceRgbTest extends ExtendedITextTest { |
| 58 | + |
| 59 | + @Test |
| 60 | + public void makeDarkerTest() { |
| 61 | + DeviceRgb rgbColor = new DeviceRgb(50, 100, 150); |
| 62 | + |
| 63 | + DeviceRgb darkerRgbColor = DeviceRgb.makeDarker(rgbColor); |
| 64 | + // check the resultant darkness of RGB items with using this multiplier |
| 65 | + float multiplier = Math.max(0f, (150f / 255 - 0.33f) / (150f / 255)); |
| 66 | + |
| 67 | + Assert.assertEquals(multiplier * (50f / 255), darkerRgbColor.getColorValue()[0], 0.0001); |
| 68 | + Assert.assertEquals(multiplier * (100f / 255), darkerRgbColor.getColorValue()[1], 0.0001); |
| 69 | + Assert.assertEquals(multiplier * (150f / 255), darkerRgbColor.getColorValue()[2], 0.0001); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void makeLighterTest() { |
| 74 | + DeviceRgb rgbColor = new DeviceRgb(50, 100, 150); |
| 75 | + |
| 76 | + DeviceRgb darkerRgbColor = DeviceRgb.makeLighter(rgbColor); |
| 77 | + // check the resultant darkness of RGB items with using this multiplier |
| 78 | + float multiplier = Math.min(1f, 150f / 255 + 0.33f) / (150f / 255); |
| 79 | + |
| 80 | + Assert.assertEquals(multiplier * (50f / 255), darkerRgbColor.getColorValue()[0], 0.0001); |
| 81 | + Assert.assertEquals(multiplier * (100f / 255), darkerRgbColor.getColorValue()[1], 0.0001); |
| 82 | + Assert.assertEquals(multiplier * (150f / 255), darkerRgbColor.getColorValue()[2], 0.0001); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void colorByAWTColorConstantTest() { |
| 87 | + // RED |
| 88 | + DeviceRgb rgbColor = new DeviceRgb(Color.RED); |
| 89 | + float[] rgbColorValue = rgbColor.getColorValue(); |
| 90 | + |
| 91 | + Assert.assertEquals(1, rgbColorValue[0], 0.0001); |
| 92 | + Assert.assertEquals(0, rgbColorValue[1], 0.0001); |
| 93 | + Assert.assertEquals(0, rgbColorValue[2], 0.0001); |
| 94 | + |
| 95 | + // GREEN |
| 96 | + rgbColor = new DeviceRgb(Color.GREEN); |
| 97 | + rgbColorValue = rgbColor.getColorValue(); |
| 98 | + |
| 99 | + Assert.assertEquals(0, rgbColorValue[0], 0.0001); |
| 100 | + Assert.assertEquals(1, rgbColorValue[1], 0.0001); |
| 101 | + Assert.assertEquals(0, rgbColorValue[2], 0.0001); |
| 102 | + |
| 103 | + // BLUE |
| 104 | + rgbColor = new DeviceRgb(Color.BLUE); |
| 105 | + rgbColorValue = rgbColor.getColorValue(); |
| 106 | + |
| 107 | + Assert.assertEquals(0, rgbColorValue[0], 0.0001); |
| 108 | + Assert.assertEquals(0, rgbColorValue[1], 0.0001); |
| 109 | + Assert.assertEquals(1, rgbColorValue[2], 0.0001); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void colorByAWTColorTest() { |
| 114 | + Color color = new Color(50, 100, 150); |
| 115 | + DeviceRgb rgbColor = new DeviceRgb(color); |
| 116 | + float[] rgbColorValue = rgbColor.getColorValue(); |
| 117 | + Assert.assertEquals(50f / 255, rgbColorValue[0], 0.0001); |
| 118 | + Assert.assertEquals(100f / 255, rgbColorValue[1], 0.0001); |
| 119 | + Assert.assertEquals(150f / 255, rgbColorValue[2], 0.0001); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + @LogMessages(messages = { |
| 124 | + @LogMessage(messageTemplate = LogMessageConstant.COLORANT_INTENSITIES_INVALID, count = 14) |
| 125 | + }) |
| 126 | + public void invalidConstructorArgumentsTest() { |
| 127 | + Assert.assertEquals(0, getSumOfColorValues(new DeviceRgb(-2f, 0f, 0f)), 0.001f); |
| 128 | + Assert.assertEquals(0, getSumOfColorValues(new DeviceRgb(0f, -2f, 0f)), 0.001f); |
| 129 | + Assert.assertEquals(0, getSumOfColorValues(new DeviceRgb(0f, 0f, -2f)), 0.001f); |
| 130 | + |
| 131 | + Assert.assertEquals(1, getSumOfColorValues(new DeviceRgb(2f, 0f, 0f)), 0.001f); |
| 132 | + Assert.assertEquals(1, getSumOfColorValues(new DeviceRgb(0f, 2f, 0f)), 0.001f); |
| 133 | + Assert.assertEquals(1, getSumOfColorValues(new DeviceRgb(0f, 0f, 2f)), 0.001f); |
| 134 | + |
| 135 | + Assert.assertEquals(0, getSumOfColorValues(new DeviceRgb(-2f, -2f, 0f)), 0.001f); |
| 136 | + Assert.assertEquals(0, getSumOfColorValues(new DeviceRgb(-2f, 0f, -2f)), 0.001f); |
| 137 | + Assert.assertEquals(0, getSumOfColorValues(new DeviceRgb(0f, -2f, -2f)), 0.001f); |
| 138 | + |
| 139 | + Assert.assertEquals(2, getSumOfColorValues(new DeviceRgb(2f, 2f, 0f)), 0.001f); |
| 140 | + Assert.assertEquals(2, getSumOfColorValues(new DeviceRgb(2f, 0f, 2f)), 0.001f); |
| 141 | + Assert.assertEquals(2, getSumOfColorValues(new DeviceRgb(0f, 2f, 2f)), 0.001f); |
| 142 | + |
| 143 | + Assert.assertEquals(0, getSumOfColorValues(new DeviceRgb(-2f, -2f, -2f)), 0.001f); |
| 144 | + Assert.assertEquals(3, getSumOfColorValues(new DeviceRgb(2f, 2f, 2f)), 0.001f); |
| 145 | + } |
| 146 | + |
| 147 | + private float getSumOfColorValues(DeviceRgb deviceRgb) { |
| 148 | + float sum = 0; |
| 149 | + float[] values = deviceRgb.getColorValue(); |
| 150 | + for (int i = 0; i < values.length; i++) { |
| 151 | + sum += values[i]; |
| 152 | + } |
| 153 | + return sum; |
| 154 | + } |
| 155 | +} |
0 commit comments