Nestharus
o-o
- Reaction score
- 84
String Colors
By Nestharus and Sevion
Docs
- What String Colors does:
This colors strings with spiffy colors by converting RGB values to hexadecimal strings.
This also supports gradients as well as segmented gradients.
A segmented gradient is a gradient that is applied across the string multiple times. Each segment is a gradient. When a segment ends, the gradient restarts. If reversal is on, the gradient will reverse itself.
For example, if the color is red and the gradient is green and the string is segmented 6 times
Code:0 1 2 3 4 5 6 red -> green -> red -> green -> red -> green -> red
- How String Colors was made:
Sevion started working on a library for coloring strings for a friend of his and I then got interested and started coding my own in parallel to his.
I'd give him my code so that he could improve his own and at the very end we decided to submit the one I coded together since mine had the original code as well as segmented gradients.
We went over the syntax and design together and added a method. I made initial docs, Sevion made drastic changes to those docs, and then I tweaked his changes.
The post, ColorHex, mixing, and Segmented Gradients were not made in parallel, those were solely by me ; \.
- Coloring a string:
[ljass]local string colored = green.apply("hello")[/ljass]
- Coloring a string with RGB:
[ljass]local string colored = Color.convert(0, 255, 0)+"hello"+"|r"[/ljass]
- Applying a gradient:
[ljass]local string colored = Color.Gradient.apply("hoho", red, green, 2, true)[/ljass]
- Changing color RGB property:
[ljass]set myColor.green = 50[/ljass]
- Working with raw colors:
[ljass]local ColorHex color = 0x00FF00[/ljass]
[ljass]set color = color.setRed(0xFF) //0xFFFF00[/ljass]
[ljass]set myString = color.apply(myString)[/ljass]
- Mixing colors:
[ljass]set mixed = Color.mix(red, green, .5) //128, 128, 0[/ljass]
- Coloring bars with ratios:
[ljass]call bar.asMix(min, max, ratio)[/ljass]
JASS:
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~ String Colors ~~ By Nestharus and Sevion ~~ Version 1.2.0.3 ~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// What is String Colors?
// -An easy way to define colors using RGB values and colorize your
// game text.
//
// API:
// -struct Color extends array
// -integer red
// Red value of the color.
//
// -integer green
// Green value of the color.
//
// -integer blue
// Blue value of the color.
//
// -readonly string color
// Retreives the color code of a color struct.
//
// -static method create takes integer red, integer green, integer blue returns thistype
// Creates a color for you to use.
//
// -static method convert takes integer red, integer green, integer blue returns string
// Converts RGB into a hexidecimal color code.
//
// -method apply takes string toBeColored returns string
// Colorizes the passed string with the struct's current color.
//
// -static method mix takes Color color, Color gradient, real weight returns nothing
// Returns a mixed color by shifting color towards the gradient where 0 =< weight <= 1
//
// -method asMix takes Color color, Color gradient, real weight returns nothing
// Sets this to a mix of color and gradient
//
// -struct Gradient extends array
// -static method apply takes string toBeColored, Color color, Color gradient, integer segments, boolean reverses returns string
// Colorizes the passed string with the appropriate color gradient in
// the amount of segments defined.
//
// -struct ColorHex extends array
// -readonly integer red
// Retreives red value of the color.
//
// -method setRed takes integer val returns integer
// Sets red value of the color.
//
// -readonly integer green
// Retreives green value of the color.
//
// -method setGreen takes integer val returns integer
// Sets green value of the color.
//
// -readonly integer blue
// Retreives blue value of the color.
//
// -method setBlue takes integer val returns integer
// Sets blue value of the color.
//
// -readonly string color
// Retreives the color code of a color struct.
//
// -static method create takes integer red, integer green, integer blue returns ColorHex
// Converts RGB into a hexidecimal integer.
//
// -method apply takes string toBeColored returns string
// Colorizes the passed string with the struct's current color.
//
// -static method mix takes ColorHex color, ColorHex gradient, real weight returns ColorHex
// Returns a mixed color by shifting color towards the gradient where 0 =< weight <= 1
//
// -struct Gradient extends array
// -static method apply takes string toBeColored, integer color, integer gradient, integer segments, boolean reverses returns string
// Colorizes the passed string with the appropriate color gradient in
// the amount of segments defined.
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
library StringColors
public keyword ColorGradient
public keyword HexColorGradient
globals
private string array hexChars
endglobals
struct ColorHex extends array
public method operator red takes nothing returns integer
return this/65536
endmethod
public method setRed takes integer val returns integer
return this-this/256*256+(this/256-this/65536*256)*256+val*65536
endmethod
public method operator green takes nothing returns integer
return this/256-this/65536*256
endmethod
public method setGreen takes integer val returns integer
return this-this/256*256+val*256+this/65536*65536
endmethod
public method operator blue takes nothing returns integer
return this-this/256*256
endmethod
public method setBlue takes integer val returns integer
return this/256*256+val
endmethod
public method operator color takes nothing returns string
return "|cff"+hexChars[this/65536]+hexChars[this/256-this/65536*256]+hexChars[this-this/256*256]
endmethod
public method apply takes string toBeColored returns string
return color+toBeColored+"|r"
endmethod
public static method create takes integer red, integer green, integer blue returns thistype
return (red*256+green)*256+blue
endmethod
public static method mix takes ColorHex color, ColorHex gradient, real weight returns ColorHex
return (R2I(color/65536-(color/65536-gradient/65536)*weight+.5)*256+R2I((color/256-color/65536*256)-((color/256-color/65536*256)-(gradient/256-gradient/65536*256))*weight+.5))*256+R2I((color-color/256*256)-((color-color/256*256)-(gradient-gradient/256*256))*weight+.5)
endmethod
public static method operator Gradient takes nothing returns HexColorGradient
return 0
endmethod
endstruct
struct Color extends array
private static integer instanceCount = 0
private static integer array recycle
private static integer recycleCount = 0
private integer redX
private integer greenX
private integer blueX
private string colorX
public static method operator Gradient takes nothing returns ColorGradient
return 0
endmethod
public static method convert takes integer red, integer green, integer blue returns string
return "|cff" + hexChars[red]+hexChars[green]+hexChars[blue]
endmethod
public method apply takes string toBeColored returns string
return colorX+toBeColored+"|r"
endmethod
public method operator red takes nothing returns integer
return redX
endmethod
public method operator red= takes integer val returns nothing
set redX = val
set colorX = convert(redX, greenX, blueX)
endmethod
public method operator green takes nothing returns integer
return greenX
endmethod
public method operator green= takes integer val returns nothing
set greenX = val
set colorX = convert(redX, greenX, blueX)
endmethod
public method operator blue takes nothing returns integer
return blueX
endmethod
public method operator blue= takes integer val returns nothing
set blueX = val
set colorX = convert(redX, greenX, blueX)
endmethod
public method operator color takes nothing returns string
return colorX
endmethod
public static method create takes integer red, integer green, integer blue returns thistype
local thistype this
if (recycleCount != 0) then
set recycleCount = recycleCount - 1
set this = recycle[recycleCount]
else
set instanceCount = instanceCount + 1
set this = instanceCount
endif
set redX = red
set greenX = green
set blueX = blue
set colorX = convert(red, green, blue)
return this
endmethod
public method destroy takes nothing returns nothing
set recycle[recycleCount] = this
set recycleCount = recycleCount + 1
endmethod
private static method onInit takes nothing returns nothing
local integer d0 = 16
local integer d1
set hexChars[0] = "0"
set hexChars[1] = "1"
set hexChars[2] = "2"
set hexChars[3] = "3"
set hexChars[4] = "4"
set hexChars[5] = "5"
set hexChars[6] = "6"
set hexChars[7] = "7"
set hexChars[8] = "8"
set hexChars[9] = "9"
set hexChars[10] = "A"
set hexChars[11] = "B"
set hexChars[12] = "C"
set hexChars[13] = "D"
set hexChars[14] = "E"
set hexChars[15] = "F"
loop
set d0 = d0 - 1
set d1 = 16
loop
set d1 = d1 - 1
set hexChars[d0*16+d1] = hexChars[d0]+hexChars[d1]
exitwhen d1 == 0
endloop
exitwhen d0 == 0
endloop
endmethod
public method asMix takes Color color, Color gradient, real weight returns nothing
set this.red = R2I(color.red-(color.red-gradient.red)*weight+.5)
set this.green = R2I(color.green-(color.green-gradient.green)*weight+.5)
set this.blue = R2I(color.blue-(color.blue-gradient.blue)*weight+.5)
endmethod
public static method mix takes Color color, Color gradient, real weight returns thistype
local thistype this
if (recycleCount != 0) then
set recycleCount = recycleCount - 1
set this = recycle[recycleCount]
else
set instanceCount = instanceCount + 1
set this = instanceCount
endif
set this.red = R2I(color.red-(color.red-gradient.red)*weight+.5)
set this.green = R2I(color.green-(color.green-gradient.green)*weight+.5)
set this.blue = R2I(color.blue-(color.blue-gradient.blue)*weight+.5)
return this
endmethod
endstruct
public struct HexColorGradient extends array
public static method apply takes string toBeColored, integer color, integer gradient, integer segments, boolean reverses returns string
local string colored //the colored string
local integer length //length of string to color
local integer position //current character position of string to color
local real addRed //how much red to add
local real addGreen //how much green to add
local real addBlue //how much blue to add
local real red //current red
local real green //current green
local real blue //current blue
local integer colorRed
local integer colorGreen
local integer colorBlue
local integer gradientRed
local integer gradientGreen
local integer gradientBlue
local integer segment //current coloring segment
local integer subPosition //sub position of segment
local integer subLength //length of current segment (needed if string can't be split entirely)
local real percent //percent of color to use
local integer flag //neg flag
//if the string exists, go on
if (toBeColored != null and toBeColored != "") then
set colorRed = color/65536
set colorGreen = color/256-color/65536*256
set colorBlue = color-color/256*256
set gradientRed = color/65536
set gradientGreen = color/256-color/65536*256
set gradientBlue = color-color/256*256
set length = StringLength(toBeColored)
set colored = ""
set flag = 0
set red = colorRed
set green = colorGreen
set blue = colorBlue
set position = 0
//if result is a single character or micro segments
//return the string as base color
if (length == 1 or segments >= length or segments <= 0) then
set colored = "|cff"+hexChars[colorRed]+hexChars[colorGreen]+hexChars[colorBlue]+toBeColored
elseif (segments > 1) then
//because subPosition starts at 0
//decrease by 1
set subLength = length/segments-1
//set sub position
set subPosition = 0
//all of the adders are just the complete as percents
//are used instead
set addRed = gradientRed - colorRed
set addGreen = gradientGreen - colorGreen
set addBlue = gradientBlue - colorBlue
//flag starts positive
set flag = 1
//segment starts at 0
set segment = 0
loop
//add colored character to colored string
set colored = colored + Color.convert(R2I(red+.5), R2I(green+.5), R2I(blue+.5))+SubString(toBeColored, position, position+1)
set position = position + 1
exitwhen position == length
//if hit end of sub, reverse direction of gradient
if (subPosition == subLength or (subPosition == 0 and flag < 1)) then
set segment = segment + 1
set subLength = (length-position)/(segments-segment)
if (reverses) then
set flag = flag * -1
if (subPosition != 0) then
set subPosition = subLength
endif
else
set subPosition = 0
endif
endif
//move ahead the sub sub string
set subPosition = subPosition + flag
//get current percentage of movement
set percent = I2R(subPosition)/subLength
//set colors by percent
set red = colorRed+addRed*percent
set green = colorGreen+addGreen*percent
set blue = colorBlue+addBlue*percent
endloop
//process single segment gradient
else
//because this starts on 0, set length to length-1
set subLength = length-1
//adders are based on total length
set addRed = I2R(gradientRed - colorRed)/subLength
set addGreen = I2R(gradientGreen - colorGreen)/subLength
set addBlue = I2R(gradientBlue - colorBlue)/subLength
loop
set colored = colored + Color.convert(R2I(red+.5), R2I(green+.5), R2I(blue+.5))+SubString(toBeColored, position, position+1)
set position = position + 1
exitwhen position == length
set red = red + addRed
set green = green + addGreen
set blue = blue + addBlue
endloop
endif
return colored+"|r"
endif
return null
endmethod
endstruct
public struct ColorGradient extends array
public static method apply takes string toBeColored, Color color, Color gradient, integer segments, boolean reverses returns string
local string colored //the colored string
local integer length //length of string to color
local integer position //current character position of string to color
local real addRed //how much red to add
local real addGreen //how much green to add
local real addBlue //how much blue to add
local real red //current red
local real green //current green
local real blue //current blue
local integer segment //current coloring segment
local integer subPosition //sub position of segment
local integer subLength //length of current segment (needed if string can't be split entirely)
local real percent //percent of color to use
local integer flag //neg flag
//if the string exists, go on
if (toBeColored != null and toBeColored != "") then
set length = StringLength(toBeColored)
set colored = ""
set flag = 0
set red = color.red
set green = color.green
set blue = color.blue
set position = 0
//if result is a single character or micro segments
//return the string as base color
if (length == 1 or segments >= length or segments <= 0) then
set colored = color.color+toBeColored
elseif (segments > 1) then
//because subPosition starts at 0
//decrease by 1
set subLength = length/segments-1
//set sub position
set subPosition = 0
//all of the adders are just the complete as percents
//are used instead
set addRed = gradient.red - color.red
set addGreen = gradient.green - color.green
set addBlue = gradient.blue - color.blue
//flag starts positive
set flag = 1
//segment starts at 0
set segment = 0
loop
//add colored character to colored string
set colored = colored + Color.convert(R2I(red+.5), R2I(green+.5), R2I(blue+.5))+SubString(toBeColored, position, position+1)
set position = position + 1
exitwhen position == length
//if hit end of sub, reverse direction of gradient
if (subPosition == subLength or (subPosition == 0 and flag < 1)) then
set segment = segment + 1
set subLength = (length-position)/(segments-segment)
if (reverses) then
set flag = flag * -1
if (subPosition != 0) then
set subPosition = subLength
endif
else
set subPosition = 0
endif
endif
//move ahead the sub sub string
set subPosition = subPosition + flag
//get current percentage of movement
set percent = I2R(subPosition)/subLength
//set colors by percent
set red = color.red+addRed*percent
set green = color.green+addGreen*percent
set blue = color.blue+addBlue*percent
endloop
//process single segment gradient
else
//because this starts on 0, set length to length-1
set subLength = length-1
//adders are based on total length
set addRed = I2R(gradient.red - color.red)/subLength
set addGreen = I2R(gradient.green - color.green)/subLength
set addBlue = I2R(gradient.blue - color.blue)/subLength
loop
set colored = colored + Color.convert(R2I(red+.5), R2I(green+.5), R2I(blue+.5))+SubString(toBeColored, position, position+1)
set position = position + 1
exitwhen position == length
set red = red + addRed
set green = green + addGreen
set blue = blue + addBlue
endloop
endif
return colored+"|r"
endif
return null
endmethod
endstruct
endlibrary