Jump to content

'guiSetText()' with two Colours


FSXTim

Recommended Posts

Posted

Hello,

I created a label, as you can see here:

uhrzeit3 = guiCreateLabel(0.1494,0.383,0.4689,0.0638,"",true,zeit_Window) 
guiSetFont(uhrzeit3,"default-bold-small") 

Colour: white

How can I do that 'London' is red? This doesn't work:

guiSetText(uhrzeit3, "Uhrzeit in #FF0000London:") 

Greets

Posted
function guiCreateColorLabel( ax, ay, bx, by,str, bool, parent ) 
    local pat = "(.-)#(%x%x%x%x%x%x)" 
    local s, e, cap, col = str:find( pat, 1 ) 
    local last = 1 
    local r,g,b 
    while s do 
        if cap == "" and col then r,g,b = tonumber( "0x"..col:sub( 1, 2 ) ), tonumber( "0x"..col:sub( 3, 4 ) ), tonumber( "0x"..col:sub( 5, 6 ) ) end 
        if s ~= 1 or cap ~= "" then 
            local w = dxGetTextWidth( cap ) 
            avc321 = guiCreateLabel( ax, ay, ax + w, by,cap,bool,parent ) 
            if not r then r = 255 end 
            if not g then g = 255 end 
            if not b then b = 255 end 
            guiLabelSetColor( avc321,r,g,b ) 
            ax = ax + w 
            r,g,b = tonumber( "0x"..col:sub( 1, 2 ) ), tonumber( "0x"..col:sub( 3, 4 ) ), tonumber( "0x"..col:sub( 5, 6 ) ) 
        end 
        last = e + 1 
        s, e, cap, col = str:find( pat, last ) 
    end 
    if last <= #str then 
        local cap = str:sub( last ) 
        local w = dxGetTextWidth( cap ) 
        local avc123 = guiCreateLabel( ax, ay, ax + w, by,cap,bool,parent ) 
        guiLabelSetColor( avc123,r or 255,g or 255,b or 255 ) 
        return avc123 
    end 
end 

If that doesn't work, then search for "guiCreateColorLabel".

P.S: I didn't make that function.

Posted

But I have to change

function guiCreateColorLabel( ax, ay, bx, by,str, bool, parent ) 

to

function guiCreateColorLabel( 0.6639,0.383,0.1826,0.0638,"",true,zeit_Window) 

right?

Greets

Posted

But don't change the function name itself.

Example:

--You use the function like this: 
guiCreateColorLabel( 0.6639,0.383,0.1826,0.0638,"",true,zeit_Window) 
  
--but define the function as it is: 
function guiCreateColorLabel( ax, ay, bx, by,str, bool, parent ) 
    local pat = "(.-)#(%x%x%x%x%x%x)" 
    local s, e, cap, col = str:find( pat, 1 ) 
    local last = 1 
    local r,g,b 
    while s do 
        if cap == "" and col then r,g,b = tonumber( "0x"..col:sub( 1, 2 ) ), tonumber( "0x"..col:sub( 3, 4 ) ), tonumber( "0x"..col:sub( 5, 6 ) ) end 
        if s ~= 1 or cap ~= "" then 
            local w = dxGetTextWidth( cap ) 
            avc321 = guiCreateLabel( ax, ay, ax + w, by,cap,bool,parent ) 
            if not r then r = 255 end 
            if not g then g = 255 end 
            if not b then b = 255 end 
            guiLabelSetColor( avc321,r,g,b ) 
            ax = ax + w 
            r,g,b = tonumber( "0x"..col:sub( 1, 2 ) ), tonumber( "0x"..col:sub( 3, 4 ) ), tonumber( "0x"..col:sub( 5, 6 ) ) 
        end 
        last = e + 1 
        s, e, cap, col = str:find( pat, last ) 
    end 
    if last <= #str then 
        local cap = str:sub( last ) 
        local w = dxGetTextWidth( cap ) 
        local avc123 = guiCreateLabel( ax, ay, ax + w, by,cap,bool,parent ) 
        guiLabelSetColor( avc123,r or 255,g or 255,b or 255 ) 
        return avc123 
    end 
end 

Posted

Okay, thanks!

But I don't understand it completely.

How would the script look like, if the text in the label is the following?

Time in [color=#FF0000]London[/color] 

'Time in' is white and 'London' is red.

Greets

Posted

Do you know hex color codes?

guiCreateColorLabel( 0.6639,0.383,0.1826,0.0638,"#FFFFFFTime in #FF0000London",true,zeit_Window) 

Posted

No, I didn't know them!

No, just fun, but I didn't know if it is able to use hex color codes in this function.

Thanks!

Greets

Posted

Yea, you're right! ;)

Still having a problem:

Error (line1): attempt to call glocal 'guiCreateColorLabel' (a nil value)

guiCreateColorLabel(0.1494,0.383,0.4689,0.0638,"#FFFFFFTime in #FF0000London",true,zeit_Window) 
  
function guiCreateColorLabel( ax, ay, bx, by,str, bool, parent ) 
    local pat = "(.-)#(%x%x%x%x%x%x)" 
    local s, e, cap, col = str:find( pat, 1 ) 
    local last = 1 
    local r,g,b 
    while s do 
        if cap == "" and col then r,g,b = tonumber( "0x"..col:sub( 1, 2 ) ), tonumber( "0x"..col:sub( 3, 4 ) ), tonumber( "0x"..col:sub( 5, 6 ) ) end 
        if s ~= 1 or cap ~= "" then 
            local w = dxGetTextWidth( cap ) 
            avc321 = guiCreateLabel( ax, ay, ax + w, by,cap,bool,parent ) 
            if not r then r = 255 end 
            if not g then g = 255 end 
            if not b then b = 255 end 
            guiLabelSetColor( avc321,r,g,b ) 
            ax = ax + w 
            r,g,b = tonumber( "0x"..col:sub( 1, 2 ) ), tonumber( "0x"..col:sub( 3, 4 ) ), tonumber( "0x"..col:sub( 5, 6 ) ) 
        end 
        last = e + 1 
        s, e, cap, col = str:find( pat, last ) 
    end 
    if last <= #str then 
        local cap = str:sub( last ) 
        local w = dxGetTextWidth( cap ) 
        local avc123 = guiCreateLabel( ax, ay, ax + w, by,cap,bool,parent ) 
        guiLabelSetColor( avc123,r or 255,g or 255,b or 255 ) 
        return avc123 
    end 
end 

Greets

Posted

The problem is, you're calling the function as soon as the script starts, and the script starts from up all the way down.

So it first meats your call to the function guiCreateColorLabel, while you haven't even defined it yet.

So you have to put the call for the function below the function declaring.

function guiCreateColorLabel( ax, ay, bx, by,str, bool, parent ) 
    local pat = "(.-)#(%x%x%x%x%x%x)" 
    local s, e, cap, col = str:find( pat, 1 ) 
    local last = 1 
    local r,g,b 
    while s do 
        if cap == "" and col then r,g,b = tonumber( "0x"..col:sub( 1, 2 ) ), tonumber( "0x"..col:sub( 3, 4 ) ), tonumber( "0x"..col:sub( 5, 6 ) ) end 
        if s ~= 1 or cap ~= "" then 
            local w = dxGetTextWidth( cap ) 
            avc321 = guiCreateLabel( ax, ay, ax + w, by,cap,bool,parent ) 
            if not r then r = 255 end 
            if not g then g = 255 end 
            if not b then b = 255 end 
            guiLabelSetColor( avc321,r,g,b ) 
            ax = ax + w 
            r,g,b = tonumber( "0x"..col:sub( 1, 2 ) ), tonumber( "0x"..col:sub( 3, 4 ) ), tonumber( "0x"..col:sub( 5, 6 ) ) 
        end 
        last = e + 1 
        s, e, cap, col = str:find( pat, last ) 
    end 
    if last <= #str then 
        local cap = str:sub( last ) 
        local w = dxGetTextWidth( cap ) 
        local avc123 = guiCreateLabel( ax, ay, ax + w, by,cap,bool,parent ) 
        guiLabelSetColor( avc123,r or 255,g or 255,b or 255 ) 
        return avc123 
    end 
end 
  
guiCreateColorLabel(0.1494,0.383,0.4689,0.0638,"#FFFFFFTime in #FF0000London",true,zeit_Window) 

Posted

@blazy

This cannot work, because the label has to be on my window and 'uhrzeit3' is not my window.

@Alpha

No errors, but no the label sais that:

'Time in' 

'London' is missing!

Greets

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...