- Joined
- Feb 23, 2019
I've made a newbie Python script that allows someone to convert a string of text into BBCode for Kiwifarms, which will make the string as a rainbow/gradient if posted on here, supposedly from start to finish (so only one rainbow).
These are some examples:
Hello! My name is We Are The Witches!!!!
Short
The blue is a bit hard to see with the current grey background, but that's the nature of the rainbow.
What a rainbowy sentence this is, I wonder if today is going to rain, so that maybe I can see more rainbows, all that would be left is the bows!
You don't need Python, you can just go to an online tool like this (Python online or whichever one you like) and paste the code in there. Without touching anything in the code, you can just "Run" the script and it will ask you for a sentence/word, type it in and press enter.
Once it finishes, you can click the "Copy to Clipboard" just below the "Run" button, and the output will be copied for you to paste on here (it's also going to copy your input at the beginning and "Process exited, etc" at the end, you can just delete that once pasted).
Someone may have noticed that you can also use it to generate HTML span tags with inline CSS, so you could make stuff like this (it obviously doesn't use JavaScript or advanced CSS, it's just basic CSS):
By the way, this is no expert syntax or anything, I'm sure there are ways to massively improve the code, and this is using the minimum amount of complexity from the Python language (barring maybe one line with RegEx). You're invited to improve this stuff and tell how you could have made it better.
If you're interested in what concept it tries to put into practice to achieve the result, is firstly by assuming that the color-codes will be on the 3-digit Hex format, with maximum saturation and lighting by only going through the (red, yellow, green, cyan, blue, magenta) channels, so that it achieves a rainbow (so no white, black, grays, or combinations of that).
The wheel that it simulates:
These are some examples:
Hello! My name is We Are The Witches!!!!
Short
The blue is a bit hard to see with the current grey background, but that's the nature of the rainbow.
What a rainbowy sentence this is, I wonder if today is going to rain, so that maybe I can see more rainbows, all that would be left is the bows!
You don't need Python, you can just go to an online tool like this (Python online or whichever one you like) and paste the code in there. Without touching anything in the code, you can just "Run" the script and it will ask you for a sentence/word, type it in and press enter.
Once it finishes, you can click the "Copy to Clipboard" just below the "Run" button, and the output will be copied for you to paste on here (it's also going to copy your input at the beginning and "Process exited, etc" at the end, you can just delete that once pasted).
Python:
import re
#############[EDIT]############# correct offset at your liking----------------------------------------------(Edit as wanted)
# 0 >>> starts with red
# 1 >>> starts with blue
# 2 >>> starts with green
offset = 0
#############[EDIT]############# which type of string you want----------------------------------------------(Edit as wanted)
# 1 >>> for HTML
# 2 >>> for Kiwifarms' BBCode
choice = 2
# for Kiwifarms' BBCode
len_per_char = len("[color=#rgb]x[/color]")
# for HTML
len_per_char_html = len('<span style="color:#rgb;">x</span>')
if choice == 1:
# HTML
start = "<span style=\"color:#"
between = ";\">"
end = "</span>"
else:
# Kiwifarms' BBCode
start = "[color=#"
between = "]"
end = "[/color]"
user_input = input("Enter your word/message (recommended length of 6-90):\n")
user_input_length = len(user_input)
user_input_no_space = ""
for x in user_input:
is_space = bool(re.search(r"[\s]", x))
if is_space:
continue
user_input_no_space += x
user_input_no_space_length = len(user_input_no_space)
# put rgb on a wheel
offset_correct_overflow = offset % 3
rainbow_cycle = 6
# six cycles (of 16), where each end overlaps with the next one
rainbow_combinations_short_hex = (16 * rainbow_cycle) - rainbow_cycle
third_fraction_value = rainbow_combinations_short_hex // 3
step = third_fraction_value // 2
# makes sure the offset does not map anything at 3 or higher
def offset_correct(value, offset):
value += offset
if value >= 3:
value -= 3
return value
# depending on the offset, they're at 0, 1, or 2 on the wheel
# on a clock, 0 is 12, 1 is 4, 2 is 8
r_wheelmap = offset_correct(0, offset_correct_overflow)
g_wheelmap = offset_correct(1, offset_correct_overflow)
b_wheelmap = offset_correct(2, offset_correct_overflow)
# puts the (0, 1, 2) mapped values into the wheel (that has 1 -to- total combinations) values
def rgb_on_wheel(wheelmap, fraction):
value = wheelmap * fraction + 1
return value
# given their position on the wheel, they correspond to a value from the total combinations' wheel (of: index-1)
r_map = rgb_on_wheel(r_wheelmap, third_fraction_value)
g_map = rgb_on_wheel(g_wheelmap, third_fraction_value)
b_map = rgb_on_wheel(b_wheelmap, third_fraction_value)
def calculation(i, rgb, step, comb):
# get the difference between the mapped channel on the wheel and the iterator/value on the wheel
difference = abs(rgb - i)
# however, if it's higher than half & because it's a circle and not a line, recalculates the "shorter" difference path through the other side
if difference > comb / 2:
difference = (comb - i) + rgb
# now if it's not higher than a step, it's always at max vale (f) (else statement)
# if it's higher than a step, but higher/equal than a double step, it's too far way so it's at min value (0) (first if statement)
# if it's higher than a step, but lower than a double step, it's a gradient of values that need to be calculated
if difference > step:
if difference >= step + step:
value = hex(0)[2:]
else:
# difference goes from 16-29 (which needs to translate to 1-14; 0 and 15 (0 and f) are already covered in other statements)
# so it means that the difference needs to be recalculated by sustracting a whole step
rec_diff = difference - step
value = hex(15 - rec_diff)[2:]
else:
value = hex(15)[2:]
return value
count = 0
user_input_index = 0
# wheel starts from this, before spinning
value_on_wheel = 1
value_on_wheel_float = value_on_wheel
adjust = rainbow_combinations_short_hex / user_input_no_space_length # user_input_length
while count < user_input_length:
space_char = bool(re.search(r"[\s]", user_input[user_input_index]))
if space_char:
print(f"{start}000{between}{user_input[user_input_index]}{end}", end="")
user_input_index += 1
count += 1
continue
r = calculation(value_on_wheel, r_map, step, rainbow_combinations_short_hex)
g = calculation(value_on_wheel, g_map, step, rainbow_combinations_short_hex)
b = calculation(value_on_wheel, b_map, step, rainbow_combinations_short_hex)
print(f"{start}{r}{g}{b}{between}{user_input[user_input_index]}{end}", end="")
user_input_index += 1
value_on_wheel_float += adjust
value_on_wheel_adjusted = round(value_on_wheel_float)
if value_on_wheel >= rainbow_combinations_short_hex:
value_on_wheel = 1
else:
value_on_wheel = value_on_wheel_adjusted
count += 1
Someone may have noticed that you can also use it to generate HTML span tags with inline CSS, so you could make stuff like this (it obviously doesn't use JavaScript or advanced CSS, it's just basic CSS):
By the way, this is no expert syntax or anything, I'm sure there are ways to massively improve the code, and this is using the minimum amount of complexity from the Python language (barring maybe one line with RegEx). You're invited to improve this stuff and tell how you could have made it better.
If you're interested in what concept it tries to put into practice to achieve the result, is firstly by assuming that the color-codes will be on the 3-digit Hex format, with maximum saturation and lighting by only going through the (red, yellow, green, cyan, blue, magenta) channels, so that it achieves a rainbow (so no white, black, grays, or combinations of that).
The wheel that it simulates: