- Joined
- Oct 12, 2011
- Messages
- 3,449
Description
Demo
A simple library used to add degradated color effect to any inputted message.
Requirements- HexString by Spinnaker
- JNGP
Code- JNGP
JASS:
library GradientText /* v3.2 */ requires HexString
/********************************************************************************************
* *
* 'Gradient Text' *
* ***** *
* by Dalvengyr *
* *
* Description: *
* This library is used to convert inputted string into the degradated one based on given *
* or specified color code. *
* *
* !! ADD_PREFIX at HexString library must be "false" !! *
* *
* Requirement: *
* - HexString by Bannar aka Spinnaker *
* hiveworkshop.com/forums/jass-resources-412/snippet-hexstring-248993/ *
* - JNGP *
* *
* APIs *
* *
* 1. Create gradations for inputed string *
* *
* function GradientText takes string s returns string *
* *
* 2. Create gradations for inputed string with specific color code and size. Size is in *
* percentage *
* *
* function GradientTextEx takes string s, string code1, string code2, integer size returns string
* *
********************************************************************************************/
globals
// Configuration //
// For identifying a color code. Don't use "|c" !!
public constant string IDENTIFIER = "#"
// You are recommended to keep this as true. Will delete any character '|'
private constant boolean DELETE = true
// Furthermore, edit them by your own risk
private constant integer LENGTH = StringLength(IDENTIFIER)
private string array String
private integer array Integer
endglobals
private function addGradation takes string s, string code1, string code2, integer len returns string
local boolean b
if len > 1 and code1 != code2 then
set String[0] = ""
set Integer[0] = 1
// Convert hex code to integer
set Integer[1] = HS2I(SubString(code1, 0, 2))
set Integer[2] = HS2I(SubString(code1, 2, 4))
set Integer[3] = HS2I(SubString(code1, 4, 6))
// Set the color interval from code1 to code2 devided by length of string
set Integer[4] = (HS2I(SubString(code2, 0, 2)) - Integer[1]) / (len + 1)
set Integer[5] = (HS2I(SubString(code2, 2, 4)) - Integer[2]) / (len + 1)
set Integer[6] = (HS2I(SubString(code2, 4, 6)) - Integer[3]) / (len + 1)
loop
exitwhen Integer[0] > len
set String[4] = SubString(s, Integer[0] - 1, Integer[0])
if String[4] != " " then
set b = true
static if DELETE then
if String[4] == "|" then
set b = false
endif
endif
if b then
// Convert integer to hex code
set String[1] = I2HS(Integer[1] + Integer[4] * Integer[0], false)
set String[2] = I2HS(Integer[2] + Integer[5] * Integer[0], false)
set String[3] = I2HS(Integer[3] + Integer[6] * Integer[0], false)
// Attach '0' if hex code only contains one letter
set Integer[8] = 1
loop
exitwhen Integer[8] > 3
if StringLength(String[Integer[8]]) == 1 then
set String[Integer[8]] = "0" + String[Integer[8]]
endif
set Integer[8] = Integer[8] + 1
endloop
set String[0] = String[0] + "|cff" + String[1] + String[2] + String[3] + String[4]
endif
else
set String[0] = String[0] + " "
endif
set Integer[0] = Integer[0] + 1
endloop
else
static if DELETE then
// Search for symbol '|' first
set Integer[0] = 0
loop
exitwhen Integer[0] > len
if SubString(s, Integer[0], Integer[0] + 1) == "|" then
set s = SubString(s, 0, Integer[0]) + SubString(s, Integer[0] + 1, len)
set len = len - 1
endif
set Integer[0] = Integer[0] + 1
endloop
endif
set String[0] = "|cff" + code1 + s
endif
return String[0]
endfunction
function GradientText takes string s returns string
local string code1 = ""
local string code2 = ""
local string grad
local integer i = 0
local integer t = 0
local integer dex1
local integer dex2
local integer len = StringLength(s)
// t is used to save last search point in the loop
// dex1(2) is used to save found hex code's position
loop
exitwhen i > len
if SubString(s, i, i + LENGTH) == IDENTIFIER then
// If first code is still empty then save found code to it
if code1 == "" then
set t = i
set dex1 = i
set code1 = SubString(s, i + LENGTH, i + LENGTH + 6)
// Remove found code from string
set s = SubString(s, 0, i) + SubString(s, i + LENGTH + 6, len)
set len = len - (LENGTH + 6)
else
set dex2 = i
set code2 = SubString(s, i + LENGTH, i + LENGTH + 6)
set s = SubString(s, 0, i) + SubString(s, i + LENGTH + 6, len)
set len = len - (LENGTH + 6)
if HS2I(code1) >= 0 and HS2I(code2) >= 0 then
set grad = addGradation(SubString(s, dex1, dex2), code1, code2, dex2 - dex1)
else
set grad = SubString(s, dex1, dex2)
debug call BJDebugMsg("Invalid color code: " + code1 + " - " + code2)
endif
// Insert the result into string s
set s = SubString(s, 0, dex1) + grad + SubString(s, dex2, len)
// Preparation to search for another hex code
set code1 = code2
set t = t + StringLength(grad)
set i = t
set len = len - (dex2 - dex1) + t
set dex1 = t
endif
endif
set i = i + 1
endloop
if code1 != "" then
if code2 == "" then
// If there is only one found hex code
if HS2I(code1) >= 0 then
// Use function addGradation to remove symbol '|' before inserting hex code
set s = SubString(s, 0, dex1) + addGradation(SubString(s, dex1, len), code1, code1, len - dex1) + "|r"
debug else
debug call BJDebugMsg("Invalid color code: " + code1 + " - " + code2)
endif
else
set s = s + "|r"
endif
endif
return s
endfunction
function GradientTextEx takes string s, string code1, string code2, integer size returns string
local boolean b = true
local integer len = StringLength(s)
local integer sz = R2I(len * (size * .01))
local integer i = 0
local integer mod
local string res = ""
// If specified code is invalid then skip the rest
if HS2I(code1) < 0 or HS2I(code2) < 0 or StringLength(code1) != 6 or StringLength(code2) != 6 then
debug call BJDebugMsg("Invalid color code: " + code1 + " - " + code2)
return s
endif
// Avoid critical error
if sz < 1 then
set sz = 1
endif
// Loop to insert hex code into string based on given size
loop
set mod = i - (i/sz) * sz
if mod == 0 then
if b then
set res = res + IDENTIFIER + code1
else
set res = res + IDENTIFIER + code2
endif
set b = not b
endif
if i <= len then
set res = res + SubString(s, i, i + 1)
endif
exitwhen i >= len and mod == 0
set i = i + 1
endloop
return GradientText(res)
endfunction
endlibrary
Demo
JASS:
// Use this function to unparse any color code
// So you can check whether the degradation color is true or not
// Example: call BJDebugMsg(UnparseColorCode(GradientText(s)))
function UnparseColorCode takes string s returns string
local integer len = StringLength(s)
local integer i = 0
local string sub
loop
exitwhen i > len
set sub = SubString(s, i, i + 2)
if sub == "|c" or sub == "|C" then
set s = SubString(s, 0, i) + GradientText_IDENTIFIER + SubString(s, i + 4, len)
endif
set i = i + 1
endloop
return s
endfunction
function Trig_Test_1_Actions takes nothing returns nothing
// Use GradientText function to degradate chat string and display the result
call DisplayTimedTextToPlayer(Player(0), 0.0, 0.0, 0.0, GradientText(GetEventPlayerChatString()))
endfunction
function InitTrig_Test takes nothing returns nothing
local string s = GradientTextEx("1234567890", "FF0000", "00FF00", 50) + " => GradientTextEx test."
set gg_trg_Test = CreateTrigger()
call TriggerRegisterPlayerChatEvent(gg_trg_Test, Player(0), "", false)
call TriggerAddAction(gg_trg_Test, function Trig_Test_1_Actions)
call DisplayTimedTextToPlayer(Player(0), 0, 0, 0, s)
call DisplayTimedTextToPlayer(Player(0), 0, 0, 0, "You may enter any message with color code such as " + GradientText_IDENTIFIER + "ff0000Hello " + GradientText_IDENTIFIER + "ffff00There" + GradientText_IDENTIFIER + "00ff00")
endfunction
Attachments
Last edited: