From acfae6721b868de490180aff7a7cdd8c74a6ca9c Mon Sep 17 00:00:00 2001 From: Cosimo Lupo Date: Mon, 17 Feb 2020 11:58:29 +0000 Subject: [PATCH] add tests for buildCORL and buildCPAL --- Tests/colorLib/__init__.py | 0 Tests/colorLib/builder_test.py | 56 ++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 Tests/colorLib/__init__.py create mode 100644 Tests/colorLib/builder_test.py diff --git a/Tests/colorLib/__init__.py b/Tests/colorLib/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/Tests/colorLib/builder_test.py b/Tests/colorLib/builder_test.py new file mode 100644 index 000000000..bd3898a8b --- /dev/null +++ b/Tests/colorLib/builder_test.py @@ -0,0 +1,56 @@ +from fontTools.colorLib import builder +from fontTools.colorLib.errors import ColorLibError +import pytest + + +def test_buildCOLR_v0(): + color_layer_lists = { + "a": [("a.color0", 0), ("a.color1", 1)], + "b": [("b.color1", 1), ("b.color0", 0)], + } + + colr = builder.buildCOLR(color_layer_lists) + + assert colr.tableTag == "COLR" + assert colr.version == 0 + assert colr.ColorLayers["a"][0].name == "a.color0" + assert colr.ColorLayers["a"][0].colorID == 0 + assert colr.ColorLayers["a"][1].name == "a.color1" + assert colr.ColorLayers["a"][1].colorID == 1 + assert colr.ColorLayers["b"][0].name == "b.color1" + assert colr.ColorLayers["b"][0].colorID == 1 + assert colr.ColorLayers["b"][1].name == "b.color0" + assert colr.ColorLayers["b"][1].colorID == 0 + + +def test_buildCPAL_v0(): + palettes = [ + [(0.68, 0.20, 0.32, 1.0), (0.45, 0.68, 0.21, 1.0)], + [(0.68, 0.20, 0.32, 0.6), (0.45, 0.68, 0.21, 0.6)], + [(0.68, 0.20, 0.32, 0.3), (0.45, 0.68, 0.21, 0.3)], + ] + + cpal = builder.buildCPAL(palettes) + + assert cpal.tableTag == "CPAL" + assert cpal.version == 0 + assert cpal.numPaletteEntries == 2 + + assert len(cpal.palettes) == 3 + assert [tuple(c) for c in cpal.palettes[0]] == [ + (82, 51, 173, 255), + (54, 173, 115, 255), + ] + assert [tuple(c) for c in cpal.palettes[1]] == [ + (82, 51, 173, 153), + (54, 173, 115, 153), + ] + assert [tuple(c) for c in cpal.palettes[2]] == [ + (82, 51, 173, 76), + (54, 173, 115, 76), + ] + + +def test_buildCPAL_palettes_different_lengths(): + with pytest.raises(ColorLibError, match="have different lengths"): + builder.buildCPAL([[(1, 1, 1, 1)], [(0, 0, 0, 1), (0.5, 0.5, 0.5, 1)]])