# Path: cv_algorithms/text.py
# def putTextAutoscale(img, txt, coords, fontFace, w, h, heightFraction=0.5, widthFraction=0.95,
#                      maxHeight=60, color=(0,0,0), thickness=2, hshift=0., vshift=0., baseline_shift=0.):
#     """
#     Like cv2.putText(), but auto-centers text on the given coordinates and automatically
#     chooses the text size for a (w*h) box around the center so that it consumes heightFraction*h height.
#     Also ensures that the text consumes no more than widthFraction*w space horizontally,
#     but in any case the text will not consume more than maxHeight pixels vertically.
#     
#     This is done by computing size via cv2.getTextSize() and shifting and scaling appropriately.
# 
#     Parameters
#     ----------
#     img : numpy ndarray
#         The image that is passed to OpenCV
#     txt : str
#         The text to render
#     coords : (int, int)
#         The coordinates where the text center should be placed
#     fontFace
#         The fontFace parameter that is passed to OpenCV
#     w : float
#         The width of the box to place the text into
#     h : float
#         The height of the box to place the text into
#     heightFraction : float
#         Represents the maximum fraction of the height of the box
#         that will be occupied by the text box
#     widthFraction : float
#         Represents the maximum fraction of the width of the box
#         that will be occupied by the text box
#     maxHeight : int
#         The maximum height of the text box in pixels
#     color
#         The font color that will be rendered
#     thickness
#         The rendering thickness
#     hshift : float
#         Horizontal shift, will be added to the x coordinate of the render position
#     vshift : float
#         Horizontal shift, will be added to the x coordinate of the render position
#     baseline_shift : float
#         Factor of the (baseline - bottom) y difference that will be added to the
#         y coordinate of the rendering position
#     """
#     # Get first size estimate. We will scale fractionally.
#     (scale1Width, scale1Height), baseline = cv2.getTextSize(txt, fontFace, 1., thickness)
#     # What are the different maximum scales that are mandated by the width and height
#     heightFractionScale = (heightFraction * h) / scale1Height
#     widthFractionScale = (widthFraction * w) / scale1Width
#     absMaxScale = maxHeight / scale1Height
#     # Combine height scale, width scale and absolute max height
#     newScale = min(heightFractionScale, widthFractionScale, absMaxScale)
#     # Now place text using putTextCenter()
#     putTextCenter(img, txt, coords, fontFace, newScale,
#                     color, thickness, hshift, vshift, baseline_shift)


import cv2
import numpy as np
import enum
from .text import putTextAutoscale
    vspace = int(0.1 * height)
    textheight = int(0.3 * height)

    h = ncolspaces * height + vspace * (ncolspaces - 1) + textheight * ncolspaces
    w = width * 3 + hspace * 2
    out = np.full((h, w), 255, dtype=np.uint8)

    for i, colorspace in enumerate(Colorspace):
        # Compute offsets
        vofs = textheight * (i + 1) + (vspace + height) * i
        hofs = lambda col: hspace * col + width * col
        textvofs = vofs - textheight / 2
        texthofs = lambda col: hofs(col) + width / 2
        # Get channels of current colorspace
        channels = colorspace.channels
        # Channel text
        chn0txt = "{} {}".format(colorspace.name, colorspace.channels[0].channel_name)
        chn1txt = "{} {}".format(colorspace.name, colorspace.channels[1].channel_name)
        chn2txt = "{} {}".format(colorspace.name, colorspace.channels[2].channel_name)
        # Extract all channels and convert to gray RGB mge
        chn0 = extract_channel(img, channels[0])
        chn1 = extract_channel(img, channels[1])
        chn2 = extract_channel(img, channels[2])
        # Copy image channels to output
        out[vofs:vofs + height, hofs(0):hofs(0) + width] = chn0
        out[vofs:vofs + height, hofs(1):hofs(1) + width] = chn1
        out[vofs:vofs + height, hofs(2):hofs(2) + width] = chn2
        # Render text
        putTextAutoscale(out, chn0txt, (texthofs(0), textvofs),
            cv2.FONT_HERSHEY_COMPLEX, width, textheight, color=0)

# Path: cv_algorithms/text.py
# def putTextAutoscale(img, txt, coords, fontFace, w, h, heightFraction=0.5, widthFraction=0.95,
#                      maxHeight=60, color=(0,0,0), thickness=2, hshift=0., vshift=0., baseline_shift=0.):
#     """
#     Like cv2.putText(), but auto-centers text on the given coordinates and automatically
#     chooses the text size for a (w*h) box around the center so that it consumes heightFraction*h height.
#     Also ensures that the text consumes no more than widthFraction*w space horizontally,
#     but in any case the text will not consume more than maxHeight pixels vertically.
#     
#     This is done by computing size via cv2.getTextSize() and shifting and scaling appropriately.
# 
#     Parameters
#     ----------
#     img : numpy ndarray
#         The image that is passed to OpenCV
#     txt : str
#         The text to render
#     coords : (int, int)
#         The coordinates where the text center should be placed
#     fontFace
#         The fontFace parameter that is passed to OpenCV
#     w : float
#         The width of the box to place the text into
#     h : float
#         The height of the box to place the text into
#     heightFraction : float
#         Represents the maximum fraction of the height of the box
#         that will be occupied by the text box
#     widthFraction : float
#         Represents the maximum fraction of the width of the box
#         that will be occupied by the text box
#     maxHeight : int
#         The maximum height of the text box in pixels
#     color
#         The font color that will be rendered
#     thickness
#         The rendering thickness
#     hshift : float
#         Horizontal shift, will be added to the x coordinate of the render position
#     vshift : float
#         Horizontal shift, will be added to the x coordinate of the render position
#     baseline_shift : float
#         Factor of the (baseline - bottom) y difference that will be added to the
#         y coordinate of the rendering position
#     """
#     # Get first size estimate. We will scale fractionally.
#     (scale1Width, scale1Height), baseline = cv2.getTextSize(txt, fontFace, 1., thickness)
#     # What are the different maximum scales that are mandated by the width and height
#     heightFractionScale = (heightFraction * h) / scale1Height
#     widthFractionScale = (widthFraction * w) / scale1Width
#     absMaxScale = maxHeight / scale1Height
#     # Combine height scale, width scale and absolute max height
#     newScale = min(heightFractionScale, widthFractionScale, absMaxScale)
#     # Now place text using putTextCenter()
#     putTextCenter(img, txt, coords, fontFace, newScale,
#                     color, thickness, hshift, vshift, baseline_shift)


# Path: cv_algorithms/colorspace.py
import cv2
import numpy as np
import enum
from .text import putTextAutoscale

    vspace = int(0.1 * height)
    textheight = int(0.3 * height)

    h = ncolspaces * height + vspace * (ncolspaces - 1) + textheight * ncolspaces
    w = width * 3 + hspace * 2
    out = np.full((h, w), 255, dtype=np.uint8)

    for i, colorspace in enumerate(Colorspace):
        # Compute offsets
        vofs = textheight * (i + 1) + (vspace + height) * i
        hofs = lambda col: hspace * col + width * col
        textvofs = vofs - textheight / 2
        texthofs = lambda col: hofs(col) + width / 2
        # Get channels of current colorspace
        channels = colorspace.channels
        # Channel text
        chn0txt = "{} {}".format(colorspace.name, colorspace.channels[0].channel_name)
        chn1txt = "{} {}".format(colorspace.name, colorspace.channels[1].channel_name)
        chn2txt = "{} {}".format(colorspace.name, colorspace.channels[2].channel_name)
        # Extract all channels and convert to gray RGB mge
        chn0 = extract_channel(img, channels[0])
        chn1 = extract_channel(img, channels[1])
        chn2 = extract_channel(img, channels[2])
        # Copy image channels to output
        out[vofs:vofs + height, hofs(0):hofs(0) + width] = chn0
        out[vofs:vofs + height, hofs(1):hofs(1) + width] = chn1
        out[vofs:vofs + height, hofs(2):hofs(2) + width] = chn2
        # Render text
        putTextAutoscale(out, chn0txt, (texthofs(0), textvofs),
            cv2.FONT_HERSHEY_COMPLEX, width, textheight, color=0)

        putTextAutoscale(out, chn1txt, (texthofs(1), textvofs),