|
| 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 | +using System; |
| 44 | +#if !NETSTANDARD1_6 |
| 45 | +using System.Drawing; |
| 46 | +#endif |
| 47 | +using iText.Test; |
| 48 | +using iText.Test.Attributes; |
| 49 | + |
| 50 | +namespace iText.Kernel.Colors { |
| 51 | + public class DeviceRgbTest : ExtendedITextTest { |
| 52 | + [NUnit.Framework.Test] |
| 53 | + public virtual void MakeDarkerTest() { |
| 54 | + DeviceRgb rgbColor = new DeviceRgb(50, 100, 150); |
| 55 | + DeviceRgb darkerRgbColor = DeviceRgb.MakeDarker(rgbColor); |
| 56 | + // check the resultant darkness of RGB items with using this multiplier |
| 57 | + float multiplier = Math.Max(0f, (150f / 255 - 0.33f) / (150f / 255)); |
| 58 | + NUnit.Framework.Assert.AreEqual(multiplier * (50f / 255), darkerRgbColor.GetColorValue()[0], 0.0001); |
| 59 | + NUnit.Framework.Assert.AreEqual(multiplier * (100f / 255), darkerRgbColor.GetColorValue()[1], 0.0001); |
| 60 | + NUnit.Framework.Assert.AreEqual(multiplier * (150f / 255), darkerRgbColor.GetColorValue()[2], 0.0001); |
| 61 | + } |
| 62 | + |
| 63 | + [NUnit.Framework.Test] |
| 64 | + public virtual void MakeLighterTest() { |
| 65 | + DeviceRgb rgbColor = new DeviceRgb(50, 100, 150); |
| 66 | + DeviceRgb darkerRgbColor = DeviceRgb.MakeLighter(rgbColor); |
| 67 | + // check the resultant darkness of RGB items with using this multiplier |
| 68 | + float multiplier = Math.Min(1f, 150f / 255 + 0.33f) / (150f / 255); |
| 69 | + NUnit.Framework.Assert.AreEqual(multiplier * (50f / 255), darkerRgbColor.GetColorValue()[0], 0.0001); |
| 70 | + NUnit.Framework.Assert.AreEqual(multiplier * (100f / 255), darkerRgbColor.GetColorValue()[1], 0.0001); |
| 71 | + NUnit.Framework.Assert.AreEqual(multiplier * (150f / 255), darkerRgbColor.GetColorValue()[2], 0.0001); |
| 72 | + } |
| 73 | + |
| 74 | +#if !NETSTANDARD1_6 |
| 75 | + [NUnit.Framework.Test] |
| 76 | + public virtual void ColorByAWTColorConstantTest() { |
| 77 | + // RED |
| 78 | + DeviceRgb rgbColor = new DeviceRgb(System.Drawing.Color.Red); |
| 79 | + float[] rgbColorValue = rgbColor.GetColorValue(); |
| 80 | + NUnit.Framework.Assert.AreEqual(1, rgbColorValue[0], 0.0001); |
| 81 | + NUnit.Framework.Assert.AreEqual(0, rgbColorValue[1], 0.0001); |
| 82 | + NUnit.Framework.Assert.AreEqual(0, rgbColorValue[2], 0.0001); |
| 83 | + // GREEN |
| 84 | + rgbColor = new DeviceRgb(System.Drawing.Color.Lime); |
| 85 | + rgbColorValue = rgbColor.GetColorValue(); |
| 86 | + NUnit.Framework.Assert.AreEqual(0, rgbColorValue[0], 0.0001); |
| 87 | + NUnit.Framework.Assert.AreEqual(1, rgbColorValue[1], 0.0001); |
| 88 | + NUnit.Framework.Assert.AreEqual(0, rgbColorValue[2], 0.0001); |
| 89 | + // BLUE |
| 90 | + rgbColor = new DeviceRgb(System.Drawing.Color.Blue); |
| 91 | + rgbColorValue = rgbColor.GetColorValue(); |
| 92 | + NUnit.Framework.Assert.AreEqual(0, rgbColorValue[0], 0.0001); |
| 93 | + NUnit.Framework.Assert.AreEqual(0, rgbColorValue[1], 0.0001); |
| 94 | + NUnit.Framework.Assert.AreEqual(1, rgbColorValue[2], 0.0001); |
| 95 | + } |
| 96 | + |
| 97 | + [NUnit.Framework.Test] |
| 98 | + public virtual void ColorByAWTColorTest() { |
| 99 | + System.Drawing.Color color = System.Drawing.Color.FromArgb(50, 100, 150); |
| 100 | + DeviceRgb rgbColor = new DeviceRgb(color); |
| 101 | + float[] rgbColorValue = rgbColor.GetColorValue(); |
| 102 | + NUnit.Framework.Assert.AreEqual(50f / 255, rgbColorValue[0], 0.0001); |
| 103 | + NUnit.Framework.Assert.AreEqual(100f / 255, rgbColorValue[1], 0.0001); |
| 104 | + NUnit.Framework.Assert.AreEqual(150f / 255, rgbColorValue[2], 0.0001); |
| 105 | + } |
| 106 | + |
| 107 | + [NUnit.Framework.Test] |
| 108 | + [LogMessage(iText.IO.LogMessageConstant.COLORANT_INTENSITIES_INVALID, Count = 14)] |
| 109 | + public virtual void InvalidConstructorArgumentsTest() { |
| 110 | + NUnit.Framework.Assert.AreEqual(0, GetSumOfColorValues(new DeviceRgb(-2f, 0f, 0f)), 0.001f); |
| 111 | + NUnit.Framework.Assert.AreEqual(0, GetSumOfColorValues(new DeviceRgb(0f, -2f, 0f)), 0.001f); |
| 112 | + NUnit.Framework.Assert.AreEqual(0, GetSumOfColorValues(new DeviceRgb(0f, 0f, -2f)), 0.001f); |
| 113 | + NUnit.Framework.Assert.AreEqual(1, GetSumOfColorValues(new DeviceRgb(2f, 0f, 0f)), 0.001f); |
| 114 | + NUnit.Framework.Assert.AreEqual(1, GetSumOfColorValues(new DeviceRgb(0f, 2f, 0f)), 0.001f); |
| 115 | + NUnit.Framework.Assert.AreEqual(1, GetSumOfColorValues(new DeviceRgb(0f, 0f, 2f)), 0.001f); |
| 116 | + NUnit.Framework.Assert.AreEqual(0, GetSumOfColorValues(new DeviceRgb(-2f, -2f, 0f)), 0.001f); |
| 117 | + NUnit.Framework.Assert.AreEqual(0, GetSumOfColorValues(new DeviceRgb(-2f, 0f, -2f)), 0.001f); |
| 118 | + NUnit.Framework.Assert.AreEqual(0, GetSumOfColorValues(new DeviceRgb(0f, -2f, -2f)), 0.001f); |
| 119 | + NUnit.Framework.Assert.AreEqual(2, GetSumOfColorValues(new DeviceRgb(2f, 2f, 0f)), 0.001f); |
| 120 | + NUnit.Framework.Assert.AreEqual(2, GetSumOfColorValues(new DeviceRgb(2f, 0f, 2f)), 0.001f); |
| 121 | + NUnit.Framework.Assert.AreEqual(2, GetSumOfColorValues(new DeviceRgb(0f, 2f, 2f)), 0.001f); |
| 122 | + NUnit.Framework.Assert.AreEqual(0, GetSumOfColorValues(new DeviceRgb(-2f, -2f, -2f)), 0.001f); |
| 123 | + NUnit.Framework.Assert.AreEqual(3, GetSumOfColorValues(new DeviceRgb(2f, 2f, 2f)), 0.001f); |
| 124 | + } |
| 125 | +#endif |
| 126 | + private float GetSumOfColorValues(DeviceRgb deviceRgb) { |
| 127 | + float sum = 0; |
| 128 | + float[] values = deviceRgb.GetColorValue(); |
| 129 | + for (int i = 0; i < values.Length; i++) { |
| 130 | + sum += values[i]; |
| 131 | + } |
| 132 | + return sum; |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments