|
| 1 | +/* |
| 2 | +This file is part of the iText (R) project. |
| 3 | +Copyright (c) 1998-2020 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 iText.Kernel; |
| 44 | +using iText.Kernel.Pdf; |
| 45 | +using iText.Kernel.Pdf.Colorspace; |
| 46 | +using iText.Test; |
| 47 | + |
| 48 | +namespace iText.Kernel.Colors { |
| 49 | + public class ColorTest : ExtendedITextTest { |
| 50 | + private const float EPS = 1e-4f; |
| 51 | + |
| 52 | + [NUnit.Framework.Test] |
| 53 | + public virtual void ConvertCmykToRgbTest() { |
| 54 | + DeviceCmyk cmyk = new DeviceCmyk(0, 0, 0, 0); |
| 55 | + DeviceRgb rgb = new DeviceRgb(255, 255, 255); |
| 56 | + NUnit.Framework.Assert.AreEqual(rgb.colorValue, Color.ConvertCmykToRgb(cmyk).colorValue); |
| 57 | + } |
| 58 | + |
| 59 | + [NUnit.Framework.Test] |
| 60 | + public virtual void ConvertRgbToCmykTest() { |
| 61 | + DeviceCmyk cmyk = new DeviceCmyk(0, 0, 0, 0); |
| 62 | + DeviceRgb rgb = new DeviceRgb(255, 255, 255); |
| 63 | + NUnit.Framework.Assert.AreEqual(cmyk.colorValue, Color.ConvertRgbToCmyk(rgb).colorValue); |
| 64 | + } |
| 65 | + |
| 66 | + [NUnit.Framework.Test] |
| 67 | + public virtual void SetColorValueIncorrectComponentsNumberTest() { |
| 68 | + NUnit.Framework.Assert.That(() => { |
| 69 | + float[] colorValues = new float[] { 0.0f, 0.5f, 0.1f }; |
| 70 | + Color color = Color.MakeColor(PdfColorSpace.MakeColorSpace(PdfName.DeviceRGB), colorValues); |
| 71 | + color.SetColorValue(new float[] { 0.1f, 0.2f }); |
| 72 | + } |
| 73 | + , NUnit.Framework.Throws.InstanceOf<PdfException>().With.Message.EqualTo(PdfException.IncorrectNumberOfComponents)) |
| 74 | +; |
| 75 | + } |
| 76 | + |
| 77 | + [NUnit.Framework.Test] |
| 78 | + public virtual void EqualsAndHashCodeTest() { |
| 79 | + float[] colorValues = new float[] { 0.0f, 0.5f, 0.1f }; |
| 80 | + Color color1 = Color.MakeColor(PdfColorSpace.MakeColorSpace(PdfName.DeviceRGB), colorValues); |
| 81 | + Color color2 = Color.MakeColor(PdfColorSpace.MakeColorSpace(PdfName.DeviceRGB), colorValues); |
| 82 | + bool result = color1.Equals(color2); |
| 83 | + NUnit.Framework.Assert.IsTrue(result); |
| 84 | + NUnit.Framework.Assert.AreEqual(color1.GetHashCode(), color2.GetHashCode()); |
| 85 | + } |
| 86 | + |
| 87 | + [NUnit.Framework.Test] |
| 88 | + public virtual void EqualsAndHashCodeNullColorSpacesTest() { |
| 89 | + float[] colorValues = new float[] { 0.0f, 0.5f, 0.1f }; |
| 90 | + Color color1 = new Color(null, colorValues); |
| 91 | + Color color2 = new Color(null, colorValues); |
| 92 | + bool result = color1.Equals(color2); |
| 93 | + NUnit.Framework.Assert.IsTrue(result); |
| 94 | + NUnit.Framework.Assert.AreEqual(color1.GetHashCode(), color2.GetHashCode()); |
| 95 | + } |
| 96 | + |
| 97 | + [NUnit.Framework.Test] |
| 98 | + public virtual void EqualsAndHashCodeNullColorValuesTest() { |
| 99 | + Color color1 = new Color(PdfColorSpace.MakeColorSpace(PdfName.DeviceRGB), null); |
| 100 | + Color color2 = new Color(PdfColorSpace.MakeColorSpace(PdfName.DeviceRGB), null); |
| 101 | + bool result = color1.Equals(color2); |
| 102 | + NUnit.Framework.Assert.IsTrue(result); |
| 103 | + NUnit.Framework.Assert.AreEqual(color1.GetHashCode(), color2.GetHashCode()); |
| 104 | + } |
| 105 | + |
| 106 | + [NUnit.Framework.Test] |
| 107 | + public virtual void NotEqualsAndHashCodeDifferentColorSpacesTest() { |
| 108 | + float[] colorValues = new float[] { 0.0f, 0.5f, 0.1f }; |
| 109 | + Color color1 = Color.MakeColor(PdfColorSpace.MakeColorSpace(PdfName.DeviceRGB), colorValues); |
| 110 | + Color color2 = Color.MakeColor(PdfColorSpace.MakeColorSpace(PdfName.DeviceGray), colorValues); |
| 111 | + bool result = color1.Equals(color2); |
| 112 | + NUnit.Framework.Assert.IsFalse(result); |
| 113 | + NUnit.Framework.Assert.AreNotEqual(color1.GetHashCode(), color2.GetHashCode()); |
| 114 | + } |
| 115 | + |
| 116 | + [NUnit.Framework.Test] |
| 117 | + public virtual void NotEqualsNullObjectTest() { |
| 118 | + float[] colorValues = new float[] { 0.0f, 0.5f, 0.1f }; |
| 119 | + Color color1 = Color.MakeColor(PdfColorSpace.MakeColorSpace(PdfName.DeviceRGB), colorValues); |
| 120 | + bool result = color1.Equals(null); |
| 121 | + NUnit.Framework.Assert.IsFalse(result); |
| 122 | + } |
| 123 | + |
| 124 | + [NUnit.Framework.Test] |
| 125 | + public virtual void NotEqualsDifferentClassesTest() { |
| 126 | + float[] colorValues = new float[] { 0.0f, 0.5f, 0.1f }; |
| 127 | + Color color1 = Color.MakeColor(PdfColorSpace.MakeColorSpace(PdfName.DeviceRGB), colorValues); |
| 128 | + DeviceCmyk cmyk = new DeviceCmyk(0, 0, 0, 0); |
| 129 | + bool result = color1.Equals(cmyk); |
| 130 | + NUnit.Framework.Assert.IsFalse(result); |
| 131 | + } |
| 132 | + |
| 133 | + [NUnit.Framework.Test] |
| 134 | + public virtual void NullColorSpaceTest() { |
| 135 | + NUnit.Framework.Assert.That(() => { |
| 136 | + float[] colorValues = new float[] { 0.0f, 0.5f, 0.1f }; |
| 137 | + Color color = Color.MakeColor(null, colorValues); |
| 138 | + } |
| 139 | + , NUnit.Framework.Throws.InstanceOf<PdfException>().With.Message.EqualTo("Unknown color space.")) |
| 140 | +; |
| 141 | + } |
| 142 | + |
| 143 | + [NUnit.Framework.Test] |
| 144 | + public virtual void MakeDeviceGrayNullColorValuesTest() { |
| 145 | + PdfColorSpace colorSpace = PdfColorSpace.MakeColorSpace(PdfName.DeviceGray); |
| 146 | + Color color = Color.MakeColor(colorSpace); |
| 147 | + NUnit.Framework.Assert.IsTrue(color is DeviceGray); |
| 148 | + NUnit.Framework.Assert.AreEqual(new float[] { 0.0f }, color.GetColorValue()); |
| 149 | + } |
| 150 | + |
| 151 | + [NUnit.Framework.Test] |
| 152 | + public virtual void MakeDeviceGrayTest() { |
| 153 | + float[] colorValues = new float[] { 0.7f, 0.5f, 0.1f }; |
| 154 | + PdfColorSpace colorSpace = PdfColorSpace.MakeColorSpace(PdfName.DeviceGray); |
| 155 | + Color color = Color.MakeColor(colorSpace, colorValues); |
| 156 | + NUnit.Framework.Assert.IsTrue(color is DeviceGray); |
| 157 | + NUnit.Framework.Assert.AreEqual(new float[] { 0.7f }, color.GetColorValue()); |
| 158 | + } |
| 159 | + |
| 160 | + [NUnit.Framework.Test] |
| 161 | + public virtual void MakeDeviceCmykNullColorValuesTest() { |
| 162 | + PdfColorSpace colorSpace = PdfColorSpace.MakeColorSpace(PdfName.DeviceCMYK); |
| 163 | + Color color = Color.MakeColor(colorSpace); |
| 164 | + NUnit.Framework.Assert.IsTrue(color is DeviceCmyk); |
| 165 | + NUnit.Framework.Assert.AreEqual(new float[] { 0.0f, 0.0f, 0.0f, 1.0f }, color.GetColorValue()); |
| 166 | + } |
| 167 | + |
| 168 | + [NUnit.Framework.Test] |
| 169 | + public virtual void MakeDeviceCmykTest() { |
| 170 | + float[] colorValues = new float[] { 0.7f, 0.5f, 0.1f, 0.3f }; |
| 171 | + PdfColorSpace colorSpace = PdfColorSpace.MakeColorSpace(PdfName.DeviceCMYK); |
| 172 | + Color color = Color.MakeColor(colorSpace, colorValues); |
| 173 | + NUnit.Framework.Assert.IsTrue(color is DeviceCmyk); |
| 174 | + NUnit.Framework.Assert.AreEqual(colorValues, color.GetColorValue()); |
| 175 | + } |
| 176 | + |
| 177 | + [NUnit.Framework.Test] |
| 178 | + public virtual void UnknownDeviceCsTest() { |
| 179 | + NUnit.Framework.Assert.That(() => { |
| 180 | + Color color = Color.MakeColor(new ColorTest.CustomDeviceCs(null)); |
| 181 | + } |
| 182 | + , NUnit.Framework.Throws.InstanceOf<PdfException>().With.Message.EqualTo("Unknown color space.")) |
| 183 | +; |
| 184 | + } |
| 185 | + |
| 186 | + [NUnit.Framework.Test] |
| 187 | + public virtual void MakeCalGrayNullColorValuesTest() { |
| 188 | + PdfDictionary dictionary = new PdfDictionary(); |
| 189 | + dictionary.Put(PdfName.Gamma, new PdfNumber(2.2)); |
| 190 | + PdfArray calGray = new PdfArray(); |
| 191 | + calGray.Add(PdfName.CalGray); |
| 192 | + calGray.Add(dictionary); |
| 193 | + PdfColorSpace colorSpace = PdfColorSpace.MakeColorSpace(calGray); |
| 194 | + Color color = Color.MakeColor(colorSpace); |
| 195 | + NUnit.Framework.Assert.IsTrue(color is CalGray); |
| 196 | + NUnit.Framework.Assert.AreEqual(new float[] { 0.0f }, color.GetColorValue()); |
| 197 | + } |
| 198 | + |
| 199 | + [NUnit.Framework.Test] |
| 200 | + public virtual void MakeCalGrayTest() { |
| 201 | + PdfDictionary dictionary = new PdfDictionary(); |
| 202 | + dictionary.Put(PdfName.Gamma, new PdfNumber(2.2)); |
| 203 | + PdfArray calGray = new PdfArray(); |
| 204 | + calGray.Add(PdfName.CalGray); |
| 205 | + calGray.Add(dictionary); |
| 206 | + PdfColorSpace colorSpace = PdfColorSpace.MakeColorSpace(calGray); |
| 207 | + float[] colorValues = new float[] { 0.7f, 0.5f, 0.1f }; |
| 208 | + Color color = Color.MakeColor(colorSpace, colorValues); |
| 209 | + NUnit.Framework.Assert.IsTrue(color is CalGray); |
| 210 | + NUnit.Framework.Assert.AreEqual(new float[] { 0.7f }, color.GetColorValue()); |
| 211 | + } |
| 212 | + |
| 213 | + [NUnit.Framework.Test] |
| 214 | + public virtual void MakeCalRgbNullColorValuesTest() { |
| 215 | + PdfDictionary dictionary = new PdfDictionary(); |
| 216 | + dictionary.Put(PdfName.Gamma, new PdfNumber(2.2)); |
| 217 | + PdfArray calRgb = new PdfArray(); |
| 218 | + calRgb.Add(PdfName.CalRGB); |
| 219 | + calRgb.Add(dictionary); |
| 220 | + PdfColorSpace colorSpace = PdfColorSpace.MakeColorSpace(calRgb); |
| 221 | + Color color = Color.MakeColor(colorSpace); |
| 222 | + NUnit.Framework.Assert.IsTrue(color is CalRgb); |
| 223 | + NUnit.Framework.Assert.AreEqual(new float[] { 0.0f, 0.0f, 0.0f }, color.GetColorValue()); |
| 224 | + } |
| 225 | + |
| 226 | + [NUnit.Framework.Test] |
| 227 | + public virtual void MakeCalRgbTest() { |
| 228 | + PdfDictionary dictionary = new PdfDictionary(); |
| 229 | + dictionary.Put(PdfName.Gamma, new PdfNumber(2.2)); |
| 230 | + PdfArray calRgb = new PdfArray(); |
| 231 | + calRgb.Add(PdfName.CalRGB); |
| 232 | + calRgb.Add(dictionary); |
| 233 | + PdfColorSpace colorSpace = PdfColorSpace.MakeColorSpace(calRgb); |
| 234 | + float[] colorValues = new float[] { 0.7f, 0.5f, 0.1f }; |
| 235 | + Color color = Color.MakeColor(colorSpace, colorValues); |
| 236 | + NUnit.Framework.Assert.IsTrue(color is CalRgb); |
| 237 | + NUnit.Framework.Assert.AreEqual(colorValues, color.GetColorValue()); |
| 238 | + } |
| 239 | + |
| 240 | + [NUnit.Framework.Test] |
| 241 | + public virtual void MakeLabNullColorValuesTest() { |
| 242 | + PdfDictionary dictionary = new PdfDictionary(); |
| 243 | + dictionary.Put(PdfName.Gamma, new PdfNumber(2.2)); |
| 244 | + PdfArray calLab = new PdfArray(); |
| 245 | + calLab.Add(PdfName.Lab); |
| 246 | + calLab.Add(dictionary); |
| 247 | + PdfColorSpace colorSpace = PdfColorSpace.MakeColorSpace(calLab); |
| 248 | + Color color = Color.MakeColor(colorSpace); |
| 249 | + NUnit.Framework.Assert.IsTrue(color is Lab); |
| 250 | + NUnit.Framework.Assert.AreEqual(new float[] { 0.0f, 0.0f, 0.0f }, color.GetColorValue()); |
| 251 | + } |
| 252 | + |
| 253 | + [NUnit.Framework.Test] |
| 254 | + public virtual void MakeLabTest() { |
| 255 | + PdfDictionary dictionary = new PdfDictionary(); |
| 256 | + dictionary.Put(PdfName.Gamma, new PdfNumber(2.2)); |
| 257 | + PdfArray calLab = new PdfArray(); |
| 258 | + calLab.Add(PdfName.Lab); |
| 259 | + calLab.Add(dictionary); |
| 260 | + PdfColorSpace colorSpace = PdfColorSpace.MakeColorSpace(calLab); |
| 261 | + float[] colorValues = new float[] { 0.7f, 0.5f, 0.1f }; |
| 262 | + Color color = Color.MakeColor(colorSpace, colorValues); |
| 263 | + NUnit.Framework.Assert.IsTrue(color is Lab); |
| 264 | + NUnit.Framework.Assert.AreEqual(colorValues, color.GetColorValue()); |
| 265 | + } |
| 266 | + |
| 267 | + [NUnit.Framework.Test] |
| 268 | + public virtual void UnknownCieBasedCsTest() { |
| 269 | + NUnit.Framework.Assert.That(() => { |
| 270 | + Color color = Color.MakeColor(new ColorTest.CustomPdfCieBasedCs(new PdfArray())); |
| 271 | + } |
| 272 | + , NUnit.Framework.Throws.InstanceOf<PdfException>().With.Message.EqualTo("Unknown color space.")) |
| 273 | +; |
| 274 | + } |
| 275 | + |
| 276 | + [NUnit.Framework.Test] |
| 277 | + public virtual void MakeDeviceNNullColorValuesTest() { |
| 278 | + PdfArray deviceN = new PdfArray(); |
| 279 | + deviceN.Add(PdfName.DeviceN); |
| 280 | + deviceN.Add(new PdfArray()); |
| 281 | + deviceN.Add(null); |
| 282 | + deviceN.Add(null); |
| 283 | + PdfColorSpace colorSpace = PdfColorSpace.MakeColorSpace(deviceN); |
| 284 | + Color color = Color.MakeColor(colorSpace); |
| 285 | + NUnit.Framework.Assert.IsTrue(color is DeviceN); |
| 286 | + NUnit.Framework.Assert.AreEqual(new float[] { }, color.GetColorValue()); |
| 287 | + } |
| 288 | + |
| 289 | + [NUnit.Framework.Test] |
| 290 | + public virtual void MakeDeviceNTest() { |
| 291 | + PdfArray deviceN = new PdfArray(); |
| 292 | + deviceN.Add(PdfName.DeviceN); |
| 293 | + deviceN.Add(new PdfArray()); |
| 294 | + deviceN.Add(null); |
| 295 | + deviceN.Add(null); |
| 296 | + PdfColorSpace colorSpace = PdfColorSpace.MakeColorSpace(deviceN); |
| 297 | + float[] colorValues = new float[] { 0.7f, 0.5f, 0.1f }; |
| 298 | + Color color = Color.MakeColor(colorSpace, colorValues); |
| 299 | + NUnit.Framework.Assert.IsTrue(color is DeviceN); |
| 300 | + NUnit.Framework.Assert.AreEqual(colorValues, color.GetColorValue()); |
| 301 | + } |
| 302 | + |
| 303 | + [NUnit.Framework.Test] |
| 304 | + public virtual void MakeIndexedNullColorValuesTest() { |
| 305 | + PdfArray indexed = new PdfArray(); |
| 306 | + indexed.Add(PdfName.Indexed); |
| 307 | + indexed.Add(new PdfArray()); |
| 308 | + indexed.Add(null); |
| 309 | + indexed.Add(null); |
| 310 | + PdfColorSpace colorSpace = PdfColorSpace.MakeColorSpace(indexed); |
| 311 | + Color color = Color.MakeColor(colorSpace); |
| 312 | + NUnit.Framework.Assert.IsTrue(color is Indexed); |
| 313 | + NUnit.Framework.Assert.AreEqual(new float[] { 0.0f }, color.GetColorValue()); |
| 314 | + } |
| 315 | + |
| 316 | + [NUnit.Framework.Test] |
| 317 | + public virtual void MakeIndexedTest() { |
| 318 | + PdfArray indexed = new PdfArray(); |
| 319 | + indexed.Add(PdfName.Indexed); |
| 320 | + indexed.Add(new PdfArray()); |
| 321 | + indexed.Add(null); |
| 322 | + indexed.Add(null); |
| 323 | + PdfColorSpace colorSpace = PdfColorSpace.MakeColorSpace(indexed); |
| 324 | + float[] colorValues = new float[] { 1.0f, 0.5f, 0.1f }; |
| 325 | + Color color = Color.MakeColor(colorSpace, colorValues); |
| 326 | + NUnit.Framework.Assert.IsTrue(color is Indexed); |
| 327 | + NUnit.Framework.Assert.AreEqual(new float[] { 1f }, color.GetColorValue()); |
| 328 | + } |
| 329 | + |
| 330 | + [NUnit.Framework.Test] |
| 331 | + public virtual void UnknownSpecialCsTest() { |
| 332 | + NUnit.Framework.Assert.That(() => { |
| 333 | + Color color = Color.MakeColor(new ColorTest.CustomPdfSpecialCs(new PdfArray())); |
| 334 | + } |
| 335 | + , NUnit.Framework.Throws.InstanceOf<PdfException>().With.Message.EqualTo("Unknown color space.")) |
| 336 | +; |
| 337 | + } |
| 338 | + |
| 339 | + private class CustomDeviceCs : PdfDeviceCs { |
| 340 | + public CustomDeviceCs(PdfName pdfObject) |
| 341 | + : base(pdfObject) { |
| 342 | + } |
| 343 | + |
| 344 | + public override int GetNumberOfComponents() { |
| 345 | + return 0; |
| 346 | + } |
| 347 | + } |
| 348 | + |
| 349 | + private class CustomPdfCieBasedCs : PdfCieBasedCs { |
| 350 | + public CustomPdfCieBasedCs(PdfArray pdfObject) |
| 351 | + : base(pdfObject) { |
| 352 | + } |
| 353 | + |
| 354 | + public override int GetNumberOfComponents() { |
| 355 | + return 0; |
| 356 | + } |
| 357 | + } |
| 358 | + |
| 359 | + private class CustomPdfSpecialCs : PdfSpecialCs { |
| 360 | + public CustomPdfSpecialCs(PdfArray pdfObject) |
| 361 | + : base(pdfObject) { |
| 362 | + } |
| 363 | + |
| 364 | + public override int GetNumberOfComponents() { |
| 365 | + return 0; |
| 366 | + } |
| 367 | + } |
| 368 | + } |
| 369 | +} |
0 commit comments