colorTriples :: Int -> [(Int, Int, Int)]
colorTriples k = baseColors ++ selectedColors ++ highlightColors ++ extraColors
  where
    baseColors                                              -- 0..16
        | k >= 17   = map (\n -> (n, n, (-1))) [1..16]
        | k >= 16   = (16, 15, (-1)):(map (\n -> (n, n, (-1))) [1..15])
        | k >= 8    = (16, 0, (-1)):(map (\n -> (n, mod n 8, (-1))) [1..15])
        | otherwise = map (\n -> (n, (-1), (-1))) [1..16]
    selectedColors  = bgColors 1 $ if k >= 16               -- 17..33
                                       then (selectionColor, backupColor)
                                       else (selectionColor', backupColor')
    highlightColors = bgColors 2 $ if k >= 16               -- 34..50
                                       then (highlightColor, backupColor)
                                       else (highlightColor', backupColor')
    extraColors
            | k >= 16   = [ cons lineNumberInt lineNumberPair
                          , cons titleBarInt titleBarPair
                          , cons treeDefaultInt treeDefaultPair
                          , cons treeActiveInt treeActivePair
                          , cons errorColorInt errorPair ]
            | k >= 8    = [ cons lineNumberInt lineNumberPair'
                          , cons titleBarInt titleBarPair'
                          , cons treeDefaultInt treeDefaultPair'
                          , cons treeActiveInt treeActivePair'
                          , cons errorColorInt errorPair' ]
            | otherwise = [ (lineNumberInt, 0, (-1))
                          , (titleBarInt, (-1), 0)
                          , (treeDefaultInt, 0, (-1))
                          , (treeActiveInt, (-1), 0)
                          , (errorColorInt, 0, (-1)) ]
      where
              cons x (y, z) = (x, y, z)
    bgColors q (col, backupCol)
        | k >= 17   = let p0 = (q*17, (-1), col)
                          f = \n -> if n == col
                                        then (n + q*17, backupCol, col)
                                        else (n + q*17, n, col)
                      in p0:(map f [1..16])
        | k >= 16   = let p0 = (q*17, (-1), col)
                          p1 = (q*17 + 16, 15, col)
                          f = \n -> if n == col
                                        then (n + q*17, backupCol, col)
                                        else (n + q*17, n, col)
                      in p0:p1:(map f [1..16])
        | k >= 8    = let p0 = (q*17, (-1), col)
                          p1 = (q*17 + 16, (-1), col)
                          f = \n -> if n == col
                                        then (n + q*17, backupCol, col )
                                        else (n + q*17, n, col )
                      in p0:p1:(map (f . (\n -> mod n 8)) [1..16])
        | otherwise = map (\n -> (n, (-1), 0)) $ map (+(q*17)) [0..16]
colorTriples :: Int -> [(Int, Int, Int)]
colorTriples k = baseColors ++ selectedColors ++ highlightColors ++ extraColors
  where
    baseColors                                              -- 0..16
        | k >= 17   = map (\n -> (n, n, (-1))) [1..16]
        | k >= 16   = (16, 15, (-1)):(map (\n -> (n, n, (-1))) [1..15])
        | k >= 8    = (16, 0, (-1)):(map (\n -> (n, mod n 8, (-1))) [1..15])
        | otherwise = map (\n -> (n, (-1), (-1))) [1..16]
    selectedColors  = bgColors 1 $ if k >= 16               -- 17..33
                                       then (selectionColor, backupColor)
                                       else (selectionColor', backupColor')
    highlightColors = bgColors 2 $ if k >= 16               -- 34..50
                                       then (highlightColor, backupColor)
                                       else (highlightColor', backupColor')
    extraColors
            | k >= 16   = [ cons lineNumberInt lineNumberPair
                          , cons titleBarInt titleBarPair
                          , cons treeDefaultInt treeDefaultPair
                          , cons treeActiveInt treeActivePair
                          , cons errorColorInt errorPair ]
            | k >= 8    = [ cons lineNumberInt lineNumberPair'
                          , cons titleBarInt titleBarPair'
                          , cons treeDefaultInt treeDefaultPair'
                          , cons treeActiveInt treeActivePair'
                          , cons errorColorInt errorPair' ]
            | otherwise = [ (lineNumberInt, 0, (-1))
                          , (titleBarInt, (-1), 0)
                          , (treeDefaultInt, 0, (-1))
                          , (treeActiveInt, (-1), 0)
                          , (errorColorInt, 0, (-1)) ]
      where
              cons x (y, z) = (x, y, z)
    bgColors q (col, backupCol)
        | k >= 17   = let p0 = (q*17, (-1), col)
                          f = \n -> if n == col
                                        then (n + q*17, backupCol, col)
                                        else (n + q*17, n, col)
                      in p0:(map f [1..16])
        | k >= 16   = let p0 = (q*17, (-1), col)
                          p1 = (q*17 + 16, 15, col)
                          f = \n -> if n == col
                                        then (n + q*17, backupCol, col)
                                        else (n + q*17, n, col)
                      in p0:p1:(map f [1..16])
        | k >= 8    = let p0 = (q*17, (-1), col)
                          p1 = (q*17 + 16, (-1), col)
                          f = \n -> if n == col
                                        then (n + q*17, backupCol, col )
                                        else (n + q*17, n, col )
                      in p0:p1:(map (f . (\n -> mod n 8)) [1..16])
        | otherwise = map (\n -> (n, (-1), 0)) $ map (+(q*17)) [0..16]
colorTriples k = baseColors ++ selectedColors ++ highlightColors ++ extraColors
  where
    baseColors                                              -- 0..16
        | k >= 17   = map (\n -> (n, n, (-1))) [1..16]
        | k >= 16   = (16, 15, (-1)):(map (\n -> (n, n, (-1))) [1..15])
        | k >= 8    = (16, 0, (-1)):(map (\n -> (n, mod n 8, (-1))) [1..15])
        | otherwise = map (\n -> (n, (-1), (-1))) [1..16]
    selectedColors  = bgColors 1 $ if k >= 16               -- 17..33
                                       then (selectionColor, backupColor)
                                       else (selectionColor', backupColor')
    highlightColors = bgColors 2 $ if k >= 16               -- 34..50
                                       then (highlightColor, backupColor)
                                       else (highlightColor', backupColor')
    extraColors
            | k >= 16   = [ cons lineNumberInt lineNumberPair
                          , cons titleBarInt titleBarPair
                          , cons treeDefaultInt treeDefaultPair
                          , cons treeActiveInt treeActivePair
                          , cons errorColorInt errorPair ]
            | k >= 8    = [ cons lineNumberInt lineNumberPair'
                          , cons titleBarInt titleBarPair'
                          , cons treeDefaultInt treeDefaultPair'
                          , cons treeActiveInt treeActivePair'
                          , cons errorColorInt errorPair' ]
            | otherwise = [ (lineNumberInt, 0, (-1))
                          , (titleBarInt, (-1), 0)
                          , (treeDefaultInt, 0, (-1))
                          , (treeActiveInt, (-1), 0)
                          , (errorColorInt, 0, (-1)) ]
      where
              cons x (y, z) = (x, y, z)
    bgColors q (col, backupCol)
        | k >= 17   = let p0 = (q*17, (-1), col)
                          f = \n -> if n == col
                                        then (n + q*17, backupCol, col)
                                        else (n + q*17, n, col)
                      in p0:(map f [1..16])
        | k >= 16   = let p0 = (q*17, (-1), col)
                          p1 = (q*17 + 16, 15, col)
                          f = \n -> if n == col
                                        then (n + q*17, backupCol, col)
                                        else (n + q*17, n, col)
                      in p0:p1:(map f [1..16])
        | k >= 8    = let p0 = (q*17, (-1), col)
                          p1 = (q*17 + 16, (-1), col)
                          f = \n -> if n == col
                                        then (n + q*17, backupCol, col )
                                        else (n + q*17, n, col )
                      in p0:p1:(map (f . (\n -> mod n 8)) [1..16])
        | otherwise = map (\n -> (n, (-1), 0)) $ map (+(q*17)) [0..16]