Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: The BBCode Gradient Text Thingy™  (Read 2752 times)

XXXXYYYY

  • Bay Watcher
  • Been a long time.
    • View Profile
The BBCode Gradient Text Thingy™
« on: December 07, 2014, 08:57:10 pm »

A while back, I was thinking about joining a forum game here, but felt too lazy to actually pick a text color. The conclusion I came to was that I should should write a set of Python functions to do it for me.

Fast forward a bit to me completing the program, an I am left with the thing I made this thread about.

Basically it is a set of functions designed to create a linear gradient between two (possibly randomized) RBG colors, and automatically color a given string letter by letter in BBCode, so it can be copy-pasted directly into the forums. In addition, it can have a bit of random noise added to the gradient, referenced as the variable 'far' in the code. I ended up writing a small code in Flask to let you guys (and myself) have a bit easier access and a far better ui, available here. The only problem is that the web version will color newlines. This doesn't happen in the python file though, so I basically have no clue what's going on.

Spoiler: Code (click to show/hide)

Dropbox of the file: ForumColorThing.py

Input:
Spoiler: Text (click to show/hide)
Color was left randomized, and far was equal to zero.
Output:
Spoiler: Text (click to show/hide)

:/ I suck at writing these.
Logged
Oooooooo. I know. ClF3. That should be a fun surprise.

XXXXYYYY

  • Bay Watcher
  • Been a long time.
    • View Profile
Re: The BBCode Gradient Text Thingy™
« Reply #1 on: December 08, 2014, 01:29:23 am »

This looks like an awesome tool!
I'll try it out sometime tomorrow.

Also, it would be neat if you can demonstrate what happens when you set far to something in the OP.
Oh shoot! I had one in there, but I must have accidentally deleted it during my editing. I'll put it back in once it stops being 1:28am and I have access to my computer.
Logged
Oooooooo. I know. ClF3. That should be a fun surprise.

kytuzian

  • Bay Watcher
    • View Profile
    • Kytuzian - Youtube
Re: The BBCode Gradient Text Thingy™
« Reply #2 on: December 08, 2014, 11:37:17 am »

Inspired by your version, I made my own, if you're curious.

Also, what do you mean by the web version colors newlines?

XXXXYYYY

  • Bay Watcher
  • Been a long time.
    • View Profile
Re: The BBCode Gradient Text Thingy™
« Reply #3 on: December 08, 2014, 01:32:26 pm »

Inspired by your version, I made my own, if you're curious.

Also, what do you mean by the web version colors newlines?
It will add the color tag over the newline so it will go from this:
Code: [Select]
{Textargablargle}
{Textargablargle}
to this (with {Textargablargle} properly colored):
Code: [Select]
{Textargablargle}[color=thing]
[/color]{Textargablargle}

All it does is add a few more characters, but when the character count is multiplied by just under 24 (because of spaces) by necessity, those few characters count.

As for your version, go ahead and post it! I'm interested to see the similarities and differences!
Logged
Oooooooo. I know. ClF3. That should be a fun surprise.

Wolfkit

  • Bay Watcher
    • View Profile
Re: The BBCode Gradient Text Thingy™
« Reply #4 on: December 08, 2014, 01:50:27 pm »

Since character count is an issue, you could alter the program such that if consecutive characters are assigned the same color it would group them under the same color tag.
So that instead of
Code: [Select]
[color=#123456]H[/color][color=#123456]e[/color][color=#123456]l[/color][color=#123457]l[/color][color=123457]o[/color] [color=#123457]w[/color][color=#123457]o[/color][color=#123458]r[/color][color=#123458]l[/color][color=#123458]d[/color]you would get
Code: [Select]
[color=#123456]Hel[/color][color=#123457]lo wo[/color][color=#123458]rld[/color]
Logged
You wanna frisk this guy? This guy with the technicolor wonder limbs? The limbs that could probably slap you on several different levels of reality?
Your tabs are just pure chaos, Wolfkit.
Sig

kytuzian

  • Bay Watcher
    • View Profile
    • Kytuzian - Youtube
Re: The BBCode Gradient Text Thingy™
« Reply #5 on: December 08, 2014, 01:59:26 pm »

My Version:
Code: [Select]
import random

class Color:
    def __init__(self, r, g, b):
        self.r = int(r)
        self.g = int(g)
        self.b = int(b)
   
    def __repr__(self):
        return "{0:02x}{1:02x}{2:02x}".format(self.r, self.g, self.b)

def clamp(value, min, max):
    if value < min:
        return min
    elif value > max:
        return max
    else:
        return value       

def random_color():
    return Color(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))

def tag_text(text, color):
    return "[color=#{0}]{1}[/color]".format(color, text)
   
def get_add(start, end, steps, step):
    return start + float(end - start) / float(steps) * float(step)

#Create a gradient for the input text from start to end
def gradient_text(text,start=Color(-1,-1,-1), end=Color(-1, -1, -1)):
    #If there was no start/end color provided, create one in random
    if start.r == -1:
        start = random_color()
    if end.r == -1:
        end = random_color()
   
    words = list(text)
    colors = []
    result = ""
   
    for i in xrange(len(words)):
        r = get_add(start.r, end.r, len(words), i)
        g = get_add(start.g, end.g, len(words), i)
        b = get_add(start.b, end.b, len(words), i)
   
        colors.append(Color(r, g, b))
   
    for i in xrange(len(words)):
        result += tag_text(words[i], colors[i])
   
    return result
   
def run(text, colors=[], color_n = 2):
    colors = [random_color() for i in xrange(color_n - len(colors))] #Ensure we have at least color_n colors

    size = int(float(len(text)) / (len(colors) - 1))
   
    result = ""
   
    for i in xrange(len(colors) - 1):
        result += gradient_text(text[:size * (i + 1)], colors[i], colors[i + 1])
       
        text = text[size * (i + 1):]
       
    f = open("result.txt", "w")
   
    f.write(result)
   
    f.close()
   
    return result

The main difference is that I added another auxiliary function, run(), which will do the process for any number of colors, and you can also use the color_n keyworded argument to have it randomly generated any number of colors.

Here's a sample of the multicolor thing:

Lorem ipsum dolor sit amet, te nec hinc postulant, eos fierent deleniti quaestio ad. Alii cibo accusam at mea, qui prima liberavisse ad. Per et brute epicuri. Ei eos paulo partiendo, cum elitr assentior te. Sea elit hendrerit id, ad pri errem eligendi maiestatis. Et qui dicta delectus concludaturque, ea viris molestie pro.

And, of course, the normal thing:
Lorem ipsum dolor sit amet, te nec hinc postulant, eos fierent deleniti quaestio ad. Alii cibo accusam at mea, qui prima liberavisse ad. Per et brute epicuri. Ei eos paulo partiendo, cum elitr assentior te. Sea elit hendrerit id, ad pri errem eligendi maiestatis. Et qui dicta delectus concludaturque, ea viris molestie pro.
« Last Edit: December 08, 2014, 02:03:07 pm by kytuzian »
Logged