From 8d8fc72cc1b05f74cf63e0e9b79350fed35b7846 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" Date: Sun, 12 May 2019 21:32:09 +0200 Subject: [PATCH] Added common function for generating colour ranges, and added test for the common file --- nw/common.py | 33 +++++++++++++++++++++++++++++++- tests/nwtools.py | 13 ++++++++++++- tests/test_common.py | 45 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 tests/test_common.py diff --git a/nw/common.py b/nw/common.py index ab413a43..b1b1b4f7 100644 --- a/nw/common.py +++ b/nw/common.py @@ -19,7 +19,8 @@ def checkString(checkValue, defaultValue, allowNone=False): if allowNone: if checkValue == None: return None if checkValue == "None": return None - if isinstance(checkValue,str): return str(checkValue) + if isinstance(checkValue,str): + return str(checkValue) return defaultValue def checkInt(checkValue, defaultValue, allowNone=False): @@ -50,3 +51,33 @@ def checkBool(checkValue, defaultValue, allowNone=False): else: return defaultValue return defaultValue + +def colRange(rgbStart, rgbEnd, nStep): + + if len(rgbStart) != 3 and len(rgbEnd) != 3 and nStep < 1: + logger.error("Cannot create colour range from given parameters") + return None + + if nStep == 1: + return rgbStart + elif nStep == 2: + return [rgbStart, rgbEnd] + + dC = [0,0,0] + for c in range(3): + cA = rgbStart[c] + cB = rgbEnd[c] + dC[c] = (cB-cA)/(nStep-1) + print(dC) + retCol = [rgbStart] + for n in range(nStep): + if n > 0 and n < nStep: + retCol.append([ + int(retCol[n-1][0] + dC[0]), + int(retCol[n-1][1] + dC[1]), + int(retCol[n-1][2] + dC[2]), + ]) + retCol[-1] = rgbEnd + print(retCol) + + return retCol diff --git a/tests/nwtools.py b/tests/nwtools.py index dc3bd24b..34063c4f 100644 --- a/tests/nwtools.py +++ b/tests/nwtools.py @@ -3,6 +3,7 @@ """ from os import path, mkdir +from itertools import chain def ensureDir(theDir): if not path.isdir(theDir): @@ -28,7 +29,7 @@ def cmpFiles(fileOne, fileTwo, ignoreLines=[]): if n+1 in ignoreLines: print("Ignoring line %d" % (n+1)) continue - + if lnOne != lnTwo: print("Diff on line %d:" % (n+1)) print(" << '%s'" % lnOne) @@ -39,3 +40,13 @@ def cmpFiles(fileOne, fileTwo, ignoreLines=[]): foTwo.close() return not diffFound + +def cmpList(listOne, listTwo): + flatOne = list(chain.from_iterable([listOne])) + flatTwo = list(chain.from_iterable([listTwo])) + if len(flatOne) != len(flatTwo): + return False + for i in range(len(flatOne)): + if flatOne[i] != flatTwo[i]: + return False + return True diff --git a/tests/test_common.py b/tests/test_common.py new file mode 100644 index 00000000..d9694249 --- /dev/null +++ b/tests/test_common.py @@ -0,0 +1,45 @@ +# -*- coding: utf-8 -*- +"""novelWriter Common Class Tester +""" + +import nw +from nw.common import * +from nwtools import cmpList + +def testCheckString(): + assert checkString(None, "NotNone",True) is None + assert checkString("None","NotNone",True) is None + assert checkString("None","NotNone",False) == "None" + assert checkString(None, "NotNone",False) == "NotNone" + assert checkString(1, "NotNone",False) == "NotNone" + assert checkString(1.0, "NotNone",False) == "NotNone" + assert checkString(True, "NotNone",False) == "NotNone" + +def testCheckInt(): + assert checkInt(None, 3,True) is None + assert checkInt("None",3,True) is None + assert checkInt(None, 3,False) == 3 + assert checkInt(1, 3,False) == 1 + assert checkInt(1.0, 3,False) == 1 + assert checkInt(True, 3,False) == 1 + +def testCheckBool(): + assert checkBool(None, 3, True) is None + assert checkBool("None", 3, True) is None + assert checkBool("True", False,False) == True + assert checkBool("False",True, False) == False + assert checkBool("Boo", None, False) is None + assert checkBool(0, None, False) == False + assert checkBool(1, None, False) == True + assert checkBool(2, None, False) is None + assert checkBool(0.0, None, False) is None + assert checkBool(1.0, None, False) is None + assert checkBool(2.0, None, False) is None + +def testColRange(): + assert colRange([0,0], [0,0], 0) is None + assert cmpList(colRange([200,50,0], [50,200,0], 1), [200,50,0]) + assert cmpList(colRange([200,50,0], [50,200,0], 2), [[200,50,0],[50,200,0]]) + assert cmpList(colRange([200,50,0], [50,200,0], 3), [[200,50,0],[125,125,0],[50,200,0]]) + assert cmpList(colRange([200,50,0], [50,200,0], 4), [[200,50,0],[150,100,0],[100,150,0],[50,200,0]]) + assert cmpList(colRange([200,50,0], [50,200,0], 5), [[200,50,0],[162,87,0],[124,124,0],[86,161,0],[50,200,0]])