[SOLVED] Rainbow font for text input

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
User avatar
Kinmoku
Miko-Class Veteran
Posts: 591
Joined: Mon Aug 11, 2014 9:39 am
Completed: One Night Stand
Projects: VIDEOVERSE
Tumblr: gamesbykinmoku
itch: kinmoku
Location: Germany
Contact:

[SOLVED] Rainbow font for text input

#1 Post by Kinmoku »

Hi all,

I want to create a child-like, multi-coloured font that's used on inputted text. It's only shown once, so normally I'd use something like "{color=#f00}Red{/color}, {color=#00ff00}Green{/color}, {color=#0000ffff}Blue{/color}" on each letter, but because this is inputted text (up to 46 characters) I don't know how to do it. Perhaps it's not possible in this situation?
Last edited by Kinmoku on Fri Jul 20, 2018 6:47 am, edited 1 time in total.

rames44
Veteran
Posts: 233
Joined: Sun May 29, 2016 4:38 pm
Contact:

Re: Rainbow font for text input

#2 Post by rames44 »

You could write a python function that would process the input string, inserting the various color definitions into it.
Or you could do something similar with a custom text tag: https://www.renpy.org/doc/html/custom_text_tags.html

User avatar
Kinmoku
Miko-Class Veteran
Posts: 591
Joined: Mon Aug 11, 2014 9:39 am
Completed: One Night Stand
Projects: VIDEOVERSE
Tumblr: gamesbykinmoku
itch: kinmoku
Location: Germany
Contact:

Re: Rainbow font for text input

#3 Post by Kinmoku »

I think this is too advanced for me ^^; I don't know where to start.

rames44
Veteran
Posts: 233
Joined: Sun May 29, 2016 4:38 pm
Contact:

Re: Rainbow font for text input

#4 Post by rames44 »

Apologies - I was AFK when I wrote my reply. Here's some sample code.
The idea is that there's a Python function named "colorize" that will take an input string
and return an output string where each of the letters in the input string is surrounded
by a {color} tag. The colors to be used are taken from the COLORIZE_COLORS array -
they're used in sequence, wrapping around to the beginning of the list if the input
string is longer than the array. You can obviously alter the list of colors as you
see fit.

Code: Select all

init python:
    # A list of colors that the "colorize" function will use in sequence
    COLORIZE_COLORS = [
        '{color=#f00}',
        '{color=#0f0}',
        '{color=#00f}',
        '{color=#ff0}',
        '{color=#f0f}',
        '{color=#0ff}',
    ]

    # A Python function that will turn 'abc' into
    # '{color}a{/color}{color}b{/color}{color}c{/color}'
    # using the sequence of colors in COLORIZE_COLORS
    def colorize(input):
        # index into the COLORIZE_COLORS
        i = 0

        # array of strings that we'll build up
        result = []

        # iterate through each letter in the input string
        for letter in input:
            # Insert a {color} tag surrounding the letter
            result.append(COLORIZE_COLORS[i])
            result.append(letter)
            result.append('{/color}')
            # move to the next color, wrapping around when we get off the end of the list
            i = (i + 1) % len(COLORIZE_COLORS)

        # Now join together all the items in the array and return the resulting string
        return ''.join(result)

image background = "#888"

label start:
    scene background

label loop:
    $ name = renpy.input('What is your name')
    $ colorized_name = colorize(name)
    "Your colorized name is [colorized_name]"
    jump loop
Hope this is useful.

User avatar
Kinmoku
Miko-Class Veteran
Posts: 591
Joined: Mon Aug 11, 2014 9:39 am
Completed: One Night Stand
Projects: VIDEOVERSE
Tumblr: gamesbykinmoku
itch: kinmoku
Location: Germany
Contact:

Re: Rainbow font for text input

#5 Post by Kinmoku »

rames44 wrote: Thu Jul 19, 2018 1:08 pm Hope this is useful.
This is INCREDIBLY useful! It works like a charm :D

I wouldn't have figured out the python bit on my own, but the rest is actually pretty straight-forward. Thank you so much! I feel like I've learnt something today :mrgreen:

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: [SOLVED] Rainbow font for text input

#6 Post by trooper6 »

The code rames44 provided works...but it isn't a custom text tag as described in the documentation: https://www.renpy.org/doc/html/custom_text_tags.html
Because it isn't a custom text tag...you can't actually use it like a text tag.
So this code makes a custom text tag that you can actually use like a text tag:

Code: Select all

define COLORIZE_COLORS = [
        '#ff0000',
        '#ff8000',
        '#ffff00',
        '#00ff00',
        '#0000ff',
        '#8000ff',
        '#ff00ff'
    ]
init python:
    #Custom Text Tag
    def rainbow_tag(tag, argument, contents):
        rv = []
        for kind, text in contents:
            if kind == renpy.TEXT_TEXT:
                for i in range(0, len(text)):
                    rv.append((renpy.TEXT_TAG, "color={}".format(COLORIZE_COLORS[i%(len(COLORIZE_COLORS))])))
                    rv.append((kind, text[i]))
                    rv.append((renpy.TEXT_TAG, "/color={}"))
            else:
                rv.append((kind, text))
        return rv

    config.custom_text_tags["rainbow"] = rainbow_tag


label start():
    #http://www.renpy.org/doc/html/custom_text_tags.html#custom-text-tags
    "Creating a custom text tag is a something doable!"
    "I've made a rainbow text tag, let's check it out."
    "{rainbow}Here I am turning all the text rainbow.{/rainbow}"
    "Maybe I only want to turn one word {rainbow}rainbow{/rainbow}."
    "And of course, you can nest text tags."
    "{rainbow}For example, all this text is rainbow. And this word is {size=40}big{/size}{/rainbow}."
    "Okay, that's it."

    return

A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

Post Reply

Who is online

Users browsing this forum: Google [Bot]