当前位置: 首页>>代码示例>>Python>>正文


Python Canvas.set_color_ansi方法代码示例

本文整理汇总了Python中caca.canvas.Canvas.set_color_ansi方法的典型用法代码示例。如果您正苦于以下问题:Python Canvas.set_color_ansi方法的具体用法?Python Canvas.set_color_ansi怎么用?Python Canvas.set_color_ansi使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在caca.canvas.Canvas的用法示例。


在下文中一共展示了Canvas.set_color_ansi方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: main

# 需要导入模块: from caca.canvas import Canvas [as 别名]
# 或者: from caca.canvas.Canvas import set_color_ansi [as 别名]
def main():
    """ Main function. """

    try:
        pig = Canvas(0, 0)
        pig.import_from_memory(STRING, "text")
        cv = Canvas(pig.get_width() * 2, pig.get_height() * 2)
    except CanvasError as err:
        sys.stderr.write("%s\n" % err)
        sys.exit(2)

    cv.blit(0, 0, pig, NullCanvas())
    pig.flip()
    cv.blit(pig.get_width(), 0, pig, NullCanvas())
    pig.flip()
    pig.flop()
    cv.blit(0, pig.get_height(), pig, NullCanvas())
    pig.flop()
    pig.rotate_180()
    cv.blit(pig.get_width(), pig.get_height(), pig, NullCanvas())

    for j in range(0, cv.get_height()):
        for i in range(0, cv.get_width(), 2):
            cv.set_color_ansi(caca.COLOR_LIGHTBLUE + (i + j) % 6,
                              caca.COLOR_DEFAULT)

            a = cv.get_attr(-1, -1)
            cv.put_attr(i, j, a)
            cv.put_attr(i+1, j, a)

    print("%s" % cv.export_to_memory('utf8'))
    cv.rotate_left()
    print("%s" % cv.export_to_memory('utf8'))
开发者ID:Kirkman,项目名称:libcaca,代码行数:35,代码来源:text.py

示例2: main

# 需要导入模块: from caca.canvas import Canvas [as 别名]
# 或者: from caca.canvas.Canvas import set_color_ansi [as 别名]
def main():
    """ Main function. """

    lst = caca.get_display_driver_list()
    cur = 0

    try:
        cv = Canvas(0, 0)
        dp = Display(cv)
    except (CanvasError, DisplayError) as err:
        sys.stderr.write("%s\n" % err)
        sys.exit(127)

    cv.set_color_ansi(caca.COLOR_WHITE, caca.COLOR_BLACK)

    while True:
        cv.put_str(1, 0, "Available drivers:")
        cur_driver = dp.get_driver()
        n = 0
        for driver, desc in lst:
            if driver == cur_driver:
                cv.put_str(2, n + 2, "%s %s (%s)" % ('*', driver, desc))
            else:
                cv.put_str(2, n + 2, "%s %s (%s)" % (' ', driver, desc))
            n += 1

        cv.put_str(2, n + 3, "Switching driver in 5 seconds")
        dp.refresh()

        if dp.get_event(caca.EVENT_KEY_PRESS, Event(), 5000000):
            break

        cur += 1
        if cur < len(lst) and lst[cur][0] == "raw":
            cur += 1
        if cur >= len(lst):
            cur = 0

        dp.set_driver(lst[cur][0])
开发者ID:Kirkman,项目名称:libcaca,代码行数:41,代码来源:driver.py

示例3: main

# 需要导入模块: from caca.canvas import Canvas [as 别名]
# 或者: from caca.canvas.Canvas import set_color_ansi [as 别名]
def main():
    """ Main function. """
    try:
        cv = Canvas(0, 0)
        dp = Display(cv)
    except (CanvasError, DisplayError) as err:
        sys.stderr.write("%s\n" % err)
        sys.exit(2)

    sprite = Canvas(0, 0)
    sprite.set_color_ansi(caca.COLOR_LIGHTRED, caca.COLOR_BLACK)
    sprite.import_from_memory(THE_PIG, "text")
    sprite.set_handle(sprite.get_width()//2, sprite.get_height()//2)

    cv.set_color_ansi(caca.COLOR_WHITE, caca.COLOR_BLUE)
    cv.put_str(0, 0, "Centered sprite")
    cv.blit(cv.get_width()//2, cv.get_height()//2, sprite, NullCanvas())

    dp.refresh()
    dp.get_event(caca.EVENT_KEY_PRESS, Event(), -1)

    sys.exit(0)
开发者ID:graingert,项目名称:libcaca,代码行数:24,代码来源:blit.py

示例4: main

# 需要导入模块: from caca.canvas import Canvas [as 别名]
# 或者: from caca.canvas.Canvas import set_color_ansi [as 别名]
def main():
    """ Main function. """

    try:
        cv = Canvas(80, 24)
        dp = Display(cv)
    except (CanvasError, DisplayError) as err:
        sys.stderr.write("%s\n" % err)
        sys.exit(127)

    cv.set_color_ansi(caca.COLOR_LIGHTGRAY, caca.COLOR_BLACK)
    cv.clear()

    for i in range(0, 16):
        if i >= 8:
            y = i + 3
        else:
            y = i + 2

        cv.set_color_ansi(caca.COLOR_LIGHTGRAY, caca.COLOR_BLACK)
        cv.printf(3, y, "ANSI %i", i)

        for j in range(0, 16):
            if j >= 8:
                x = 13 + (j * 4)
            else:
                x = 12 + (j * 4)
            if i >= 8:
                y = i + 3
            else:
                y = i + 2

            cv.set_color_ansi(i, j)
            cv.put_str(x, y, "Aaホ")

    cv.set_color_ansi(caca.COLOR_LIGHTGRAY, caca.COLOR_BLACK)
    cv.put_str(3, 20, "This is bold    This is blink    This is italics    This is underline")
    cv.set_attr(caca.STYLE_BOLD)
    cv.put_str(3 + 8, 20, "bold")
    cv.set_attr(caca.STYLE_BLINK)
    cv.put_str(3 + 24, 20, "blink")
    cv.set_attr(caca.STYLE_ITALICS)
    cv.put_str(3 + 41, 20, "italics")
    cv.set_attr(caca.STYLE_UNDERLINE)
    cv.put_str(3 + 60, 20, "underline")

    dp.refresh()
    dp.get_event(caca.EVENT_KEY_PRESS, Event(), -1)
开发者ID:Kirkman,项目名称:libcaca,代码行数:50,代码来源:colors.py

示例5: main

# 需要导入模块: from caca.canvas import Canvas [as 别名]
# 或者: from caca.canvas.Canvas import set_color_ansi [as 别名]
def main():
    """ Main function. """

    try:
        cv = Canvas(8, 2)
    except CanvasError as err:
        sys.stderr.write("%s\n" % err)
        sys.exit(127)

    cv.set_color_ansi(caca.COLOR_WHITE, caca.COLOR_BLACK)
    cv.put_str(0, 0, "ABcde")
    cv.set_color_ansi(caca.COLOR_LIGHTRED, caca.COLOR_BLACK)
    cv.put_str(5, 0, "\\o/")
    cv.set_color_ansi(caca.COLOR_WHITE, caca.COLOR_BLUE)
    cv.put_str(0, 1, "&$âøÿØ?!")

    fonts = caca.get_font_list()
    if not fonts:
        sys.stderr.write("libcaca was compiled without any fonts\n")
        sys.exit(127)

    try:
        f = Font(fonts[0])
    except FontError as err:
        sys.stderr.write("%s\n" % err)
        sys.exit(127)

    w = cv.get_width() * f.get_width()
    h = cv.get_height() * f.get_height()
    buf = ctypes.c_buffer(4 * w * h)

    cv.render(f, buf, w, h, 4 * w)

    cv.set_size(80, 32)
    try:
        dp = Display(cv)
    except DisplayError as err:
        sys.stderr.write("%s\n" % err)
        sys.exit(127)

    try:
        if sys.byteorder == 'big':
            dit = Dither(32, w, h, 4 * w, 0xff0000, 0xff00, 0xff, 0xff000000)
        else:
            dit = Dither(32, w, h, 4 * w, 0xff00, 0xff0000, 0xff000000, 0xff)

        dit.bitmap(cv, 0, 0, cv.get_width(), cv.get_height(), buf)
    except DitherError as err:
        sys.stderr.write("%s\n" % err)
        sys.exit(127)
    else:
        dp.refresh()

    dp.get_event(caca.EVENT_KEY_PRESS, Event(), -1)
开发者ID:Kirkman,项目名称:libcaca,代码行数:56,代码来源:font.py

示例6: main

# 需要导入模块: from caca.canvas import Canvas [as 别名]
# 或者: from caca.canvas.Canvas import set_color_ansi [as 别名]
def main():
    """ Main function. """

    try:
        cv = Canvas(0, 0)
    except CanvasError as err:
        sys.stderr.write("%s\n" % err)
        sys.exit(2)

    for idx in range(1, 200):
        cv.create_frame(idx)

    sys.stderr.write("canvas created, size is %dx%d\n" \
                        % (cv.get_width(), cv.get_height()))

    cv.set_size(150, 80)
    sys.stderr.write("canvas expanded, size is %dx%d\n" \
                        % (cv.get_width(), cv.get_height()))

    for idx in range(0, 16):
        cv.set_frame(idx)
        cv.set_color_ansi(caca.COLOR_WHITE, idx)
        cv.fill_box(0, 0, 40, 15, ':')
        cv.set_color_ansi(caca.COLOR_WHITE, caca.COLOR_BLUE)
        cv.put_str((idx * 5) // 2, idx, "カカ")
        cv.set_color_ansi(caca.COLOR_DEFAULT, caca.COLOR_TRANSPARENT)

    cv.set_size(41, 16)
    sys.stderr.write("canvas shrinked, size is %dx%d\n" \
                        % (cv.get_width(), cv.get_height()))

    try:
        dp = Display(cv)
    except DisplayError as err:
        sys.stderr.write("%s\n" % err)
        sys.exit(2)

    dp.set_time(50000)
    sys.stderr.write("display attached, size is %dx%d\n" \
                        % (cv.get_width(), cv.get_height()))

    n = 0
    while not dp.get_event(caca.EVENT_KEY_PRESS, Event(), 0):
        cv.set_frame(n % 16)
        dp.refresh()
        n += 1
开发者ID:graingert,项目名称:libcaca,代码行数:48,代码来源:frames.py

示例7: to_ascii

# 需要导入模块: from caca.canvas import Canvas [as 别名]
# 或者: from caca.canvas.Canvas import set_color_ansi [as 别名]
def to_ascii(
    img,
    width=60,
    height=None,
    font_width=6,
    font_height=10,
    brightness=1.0,
    contrast=1.0,
    gamma=1.0,
    ditalgo="fstein",
    exformat="ansi",
        charset="ascii"):
    """
    Takes a file-pointer to an image and converts it to ASCII.
        Options:
          -h, --help                      This help
          -v, --version                   Version of the program
          -W, --width=WIDTH               Width of resulting image
          -H, --height=HEIGHT             Height of resulting image
          -x, --font-width=WIDTH          Width of output font
          -y, --font-height=HEIGHT        Height of output font
          -b, --brightness=BRIGHTNESS     Brightness of resulting image
          -c, --contrast=CONTRAST         Contrast of resulting image
          -g, --gamma=GAMMA               Gamma of resulting image
          -d, --dither=DITHER             Dithering algorithm to use
          -f, --format=FORMAT             Format of the resulting image
          -C, --charset=CHARSET           Charset of the resulting image
        DITHER (ditalgo) list:
          - none: no dithering
          - ordered2: 2x2 ordered dithering
          - ordered4: 4x4 ordered dithering
          - ordered8: 8x8 orederd dithering
          - random: random dithering
          - fstein: Floyd-Steinberg dithering
        FORMAT (exformat) list:
          - caca: native libcaca format
          - ansi: ANSI
          - utf8: UTF-8 with ANSI escape codes
          - utf8cr: UTF-8 with ANSI escape codes and MS-DOS \\r
          - html: HTML
          - html3: backwards-compatible HTML
          - bbfr: BBCode (French)
          - irc: IRC with mIRC colours
          - ps: PostScript document
          - svg: SVG vector image
          - tga: TGA image
          - troff: troff source
        CHARSET (charset) list:
          - ascii: use only ascii character
          - shades: use unicode character
          - blocks: use unicode quarter-cell combinations
    """
    img = Image.open(img)

    if "shades" or "blocks" in charset:
        exformat = "utf8"  # Will not work in ascii mode

    # Explicitly encode argument strings as ASCII
    ditalgo = ditalgo.encode('ascii')
    exformat = exformat.encode('ascii')
    charset = charset.encode('ascii')

    # Set height to some proportion
    if height is None:
        height = round(width * img.size[1] * font_width / img.size[0] / font_height)

    # Setup the canvas
    cv = Canvas(width, height)
    cv.set_color_ansi(caca.COLOR_DEFAULT, caca.COLOR_TRANSPARENT)

    #########################
    #### Begin Dithering ####
    #########################
    RMASK = 0x00ff0000
    GMASK = 0x0000ff00
    BMASK = 0x000000ff
    AMASK = 0xff000000
    BPP = 32
    DEPTH = 4

    if img.mode == 'RGB':
        img = img.convert('RGBA')
    #reorder rgba
    if img.mode == 'RGBA':
        r, g, b, a = img.split()
        img = Image.merge("RGBA", (b, g, r, a))
    dit = Dither(BPP, img.size[0], img.size[1], DEPTH * img.size[0], RMASK, GMASK, BMASK, AMASK)

    # print(dit.get_algorithm_list());
    # import ipdb; ipdb.set_trace()
    dit.set_algorithm(ditalgo)
    dit.set_brightness(brightness)
    dit.set_gamma(gamma)
    dit.set_contrast(contrast)
    dit.set_charset(charset)
    dit.bitmap(cv, 0, 0, width, height, img.tobytes())

    #########################
    #### End Dithering ######
    #########################
#.........这里部分代码省略.........
开发者ID:chanzong,项目名称:createvhost,代码行数:103,代码来源:img2txt.py

示例8: main

# 需要导入模块: from caca.canvas import Canvas [as 别名]
# 或者: from caca.canvas.Canvas import set_color_ansi [as 别名]
def main():
    """ Main function. """

    events = []
    quit = 0
    quit_string = ["", "q", "qu", "qui", "quit"]

    try:
        cv = Canvas(80, 24)
        dp = Display(cv)
    except (CanvasError, DisplayError) as err:
        sys.stderr.write("%s\n" % err)
        sys.exit(127)

    h = cv.get_height() - 1

    cv.set_color_ansi(caca.COLOR_WHITE, caca.COLOR_BLUE)
    cv.draw_line(0, 0, cv.get_width() - 1, 0, ' ')
    cv.draw_line(0, h, cv.get_width() - 1, h, ' ')
    cv.put_str(0, h, "Type \"quit\" to exit")

    dp.refresh()

    while quit < 4:
        ev = Event()
        if dp.get_event(caca.EVENT_ANY, ev, -1):
            if ev.get_type() == caca.EVENT_KEY_PRESS:
                key = ev.get_key_ch()
                if key == ord('u') and quit == 1:
                    quit += 1
                elif key == ord('i') and quit == 2:
                    quit += 1
                elif key == ord('t') and quit == 3:
                    quit += 1
                elif key == ord('q'):
                    quit = 1
                else:
                    quit = 0

            events.append(ev)

        cv.set_color_ansi(caca.COLOR_LIGHTGRAY, caca.COLOR_BLACK)
        cv.clear()

        #print current event
        cv.set_color_ansi(caca.COLOR_WHITE, caca.COLOR_BLUE)
        cv.draw_line(0, 0, cv.get_width() - 1, 0, ' ')
        if events:
            print_event(cv, 0, 0, events[-1])

        cv.draw_line(0, h, cv.get_width() - 1, h, ' ')
        cv.put_str(0, h, "Type \"quit\" to exit: %s" % quit_string[quit])

        #print previous events
        cv.set_color_ansi(caca.COLOR_WHITE, caca.COLOR_BLACK)
        counts = list(range(0, len(events)-1))
        counts.reverse()
        if len(events) > 1:
            j = 0
            for i in counts:
                if j < h - 1 and events[i].get_type():
                    print_event(cv, 0, ((len(events) - 1) - i), events[i])
                j += 1

        dp.refresh()
开发者ID:Kirkman,项目名称:libcaca,代码行数:67,代码来源:event.py

示例9: Canvas

# 需要导入模块: from caca.canvas import Canvas [as 别名]
# 或者: from caca.canvas.Canvas import set_color_ansi [as 别名]
    if not width and not height:
        width = 60
        height = width * img.size[1] * font_width / img.size[0] / font_height
    elif width and not height:
        height = width * img.size[1] * font_width / img.size[0] / font_height
    elif not width and height:
        width = height * img.size[0] * font_height / img.size[1] / font_width

    #init canvas
    try:
        cv = Canvas(width, height)
    except CanvasError, err:
        sys.stderr.write("%s\n" % err)
        sys.exit(127)

    cv.set_color_ansi(caca.COLOR_DEFAULT, caca.COLOR_TRANSPARENT)

    #init dither
    try:
        #convert rgb to rgba
        if img.mode == 'RGB':
            img = img.convert('RGBA')
        #reorder rgba
        if img.mode == 'RGBA':
            r, g, b, a = img.split()
            img = Image.merge("RGBA", (b, g, r, a))

        dit = Dither(BPP, img.size[0], img.size[1], DEPTH * img.size[0],
                        RMASK, GMASK, BMASK, AMASK)
    except DitherError, err:
        sys.stderr.write("%s\n" % err)
开发者ID:gitpan,项目名称:Term-Caca,代码行数:33,代码来源:img2txt.py

示例10: Widget

# 需要导入模块: from caca.canvas import Canvas [as 别名]
# 或者: from caca.canvas.Canvas import set_color_ansi [as 别名]

#.........这里部分代码省略.........
        Lookup the relevant style stored in the app-level spec
        style_target - the portion of the widget's spec
        which contains the style, e.g. "border"
        value - the specific style portion we want: foreground,
        background, etc.
        """
        # TODO We should cache these values on widget init, so we dont
        # look them up every time we draw a damn widget
        spec_doc = self.get_spec_doc(check_against_spec)
        # logging.debug("Looking up style " + spec_doc[style_target]["style"] \
        #                   + " for target " + style_target)
        style_name = spec_doc[style_target]["style"]
        style = filter(lambda x: x["name"] == style_name,
                       self.app.styles)[0]
        # logging.debug("Got App style: " + str(style))
        color_value_for_style_element = style[value]
        # logging.debug("got style value: " + str(color_value_for_style_element))
        return color_value_for_style_element

    def style_specifies(self,
                        style_target,
                        element,
                        value=None,
                        check_against_spec=False):
        style = self.get_style_for_spec_section(style_target,
                                                check_against_spec)
        if value == None:
            return element in style.keys()
        else:
            return element in style.keys() and style[element] == value

    def set_canvas_color_per_style_for(self, style_target):
        if self.style_specifies(style_target, "reverse", True):
            self.canvas.set_color_ansi(
                self.style_value_for(style_target, "bgColor"),
                self.style_value_for(style_target, "fgColor"))
        else:
            self.canvas.set_color_ansi(
                self.style_value_for(style_target, "fgColor"),
                self.style_value_for(style_target, "bgColor"))


    # Drawing methods
    # ---------------------------
    # Actual manipulation of the widget canvas goes here
    ###############################################################

    def draw_line_buffer(self):
        line_start = copy(self.text_origin)
        self.set_canvas_color_per_style_for("contents")
        for line in self.buffer.get_visible_slice():
            self.canvas.put_str(line_start[0],
                                line_start[1],
                                line)
            line_start[1] += 1

    def draw_border(self):
        if self.specifies("border"):
            char = self.border["character"]
            self.set_canvas_color_per_style_for("border")
            self.canvas.draw_box(0, 0, self.current_state["width"],
                                 self.current_state["height"],str(char))

    def draw(self):
        """
        This is called once every time through the main loop.
开发者ID:chazu,项目名称:jinxes,代码行数:70,代码来源:widget.py

示例11: LibcacaServer

# 需要导入模块: from caca.canvas import Canvas [as 别名]
# 或者: from caca.canvas.Canvas import set_color_ansi [as 别名]
class LibcacaServer(KinematicServer):
    def __init__(self, *args, **kwargs):
        """
        """
        KinematicServer.__init__(self, *args, **kwargs)
        self.robots = [r for (name, r) in self.elements.items()
                       if isinstance(r, Robot)]
        self.cv = Canvas()

        self.dp = Display(self.cv)
        self.ev = Event()
        self.quit = False
        self.width = self.cv.get_width()
        self.height = self.cv.get_height()
        self.camera = Camera(self, 640 , 480)
        self.camera.translation = [3.5, 0, 1]
        self.camera.init()
        self.fps = -1.0
        self.frames = 0
        self.last_t = 0

    def compute_fps(self):
        now = time.time()
        if self.last_t == 0:
            self.last_t = now
            return
        PER = 2.0
        if now - self.last_t >= PER:
            self.fps = self.frames / PER
            self.frames = 0
            self.last_t = now
            return
        else:
            self.frames += 1


    def key_cb(self):
        UP, DOWN, LEFT, RIGHT = 273,274,275,276
        if self.dp.get_event(caca.EVENT_KEY_PRESS, self.ev, 0):
            ch = self.ev.get_key_ch()
            if ch == ord('q'):
                self.quit = True
            elif ch == UP:
                self.camera.rotate(0, 1)
            elif ch == DOWN:
                self.camera.rotate(0, -1)
            elif ch == LEFT:
                self.camera.rotate(1, 0)
            elif ch == RIGHT:
                self.camera.rotate(-1, 0)


    def project(self, p):
        u, v = self.camera.project(p)
        up, vp = int(u*self.width/self.camera.width), int((v)*self.height/self.camera.height)
        return up, self.height - vp


    def draw_floor(self):
        lines=[]
        L = 5
        w = 1
        N = int(L/w)
        self.cv.set_color_ansi(caca.COLOR_WHITE, caca.COLOR_BLACK)

        for i in range(-N,N+1):
            lines.append([i*w, L,0.005])
            lines.append([i*w,-L,0.005])

            lines.append([L    , i*w,0.005])
            lines.append([-L   , i*w,0.005] )


        for i  in range(len(lines)/2):
            p1 = lines[2*i]
            p2 = lines[2*i+1]
            u1, v1 = self.project(p1)
            u2, v2 = self.project(p2)
            self.cv.draw_thin_line(int(u1), int(v1), int(u2), int(v2))

    def draw_robot(self, r):
        self.cv.set_color_ansi(caca.COLOR_GREEN, caca.COLOR_BLACK)
        for j in r.moving_joint_list:
            u, v = self.project(j.T[:3,3])
            radius = 0
            if j.parent:
                up, vp = self.project(j.parent.T[:3,3])
                self.cv.draw_thin_line(int(u), int(v), int(up), int(vp))

        self.cv.set_color_ansi(caca.COLOR_RED, caca.COLOR_BLACK)
        for j in r.moving_joint_list:
            u, v = self.project(j.T[:3,3])
            self.cv.draw_circle(u, v, radius, '@')

    def run(self):
        try:
            while not self.quit:
                self.key_cb()
                self.compute_fps()
                self.cv.clear()
#.........这里部分代码省略.........
开发者ID:duongdang,项目名称:robot-viewer,代码行数:103,代码来源:libcaca_server.py


注:本文中的caca.canvas.Canvas.set_color_ansi方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。