Skip to content

Commit b4fcf01

Browse files
committed
Improve gradient visuals
1 parent 3330573 commit b4fcf01

File tree

2 files changed

+94
-65
lines changed

2 files changed

+94
-65
lines changed

lua/custom_chat/client/block_types.lua

Lines changed: 76 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,44 @@ function Create.Embed( url, panel )
280280
return table.concat( lines, "\n" )
281281
end
282282

283+
--- Returns JS code that creates gradient text
284+
function Create.Gradient( text, font, colorA, colorB, elementName )
285+
elementName = elementName or "elGradient"
286+
287+
-- Gradient container
288+
local lines = { Create.Element( "span", elementName ) }
289+
Append( lines, "%s.className = 'gradient-container';", elementName )
290+
291+
if IsStringValid( font ) then
292+
Append( lines, "%s.style.fontFamily = '%s';", font, elementName )
293+
end
294+
295+
-- Gradient background/glow
296+
Append( lines, Create.Element( "span", "elGradientGlow", elementName ) )
297+
Append( lines, "elGradientGlow.className = 'gradient-bg';" )
298+
Append( lines, "elGradientGlow.textContent = '%s';", text )
299+
300+
-- Use the combined colors for the glow effect
301+
local h, s, l = ColorToHSL( Color(
302+
( colorA.r + colorB.r ) * 0.5,
303+
( colorA.g + colorB.g ) * 0.5,
304+
( colorA.b + colorB.b ) * 0.5
305+
) )
306+
307+
local colorGlow = ColorToRGB( HSLToColor( h, s, l * 0.5 ) )
308+
309+
Append( lines, "elGradientGlow.style.color = '%s';", colorGlow )
310+
Append( lines, "elGradientGlow.style.textShadow = '0px 0px 0.2em %s';", colorGlow )
311+
312+
-- Gradient foreground/text
313+
Append( lines, Create.Element( "span", "elGradientText", elementName ) )
314+
Append( lines, "elGradientText.className = 'gradient-fg';" )
315+
Append( lines, "elGradientText.textContent = '%s';", text )
316+
Append( lines, "elGradientText.style.backgroundImage = '-webkit-linear-gradient(left, %s, %s)';", ColorToRGB( colorA ), ColorToRGB( colorB ) )
317+
318+
return table.concat( lines, "\n" )
319+
end
320+
283321
--[[
284322
Steam avatar fetcher
285323
]]
@@ -431,77 +469,67 @@ blocks["string"] = function( value, ctx )
431469
end
432470

433471
blocks["player"] = function( value, ctx )
434-
local lines = {}
435-
436-
if not value.isBot and ctx.panel.displayAvatars then
437-
lines[#lines + 1] = Create.Image( CustomChat.FetchUserAvatarURL( value.id64, ctx.panel ), nil, "avatar ply-" .. value.id64 )
438-
end
439-
440-
lines[#lines + 1] = Create.Element( "span", "elPlayer" )
441-
Append( lines, "elPlayer.textContent = '%s';", SafeString( value.name ) )
442-
443-
if not value.isBot then
444-
Append( lines, "elPlayer._playerData = '%s';", util.TableToJSON( {
445-
steamId = value.id,
446-
steamId64 = value.id64
447-
} ) )
448-
449-
Append( lines, "elPlayer.style.cursor = 'pointer';" )
450-
Append( lines, "elPlayer.clickableText = true;" )
451-
end
452-
453-
if IsStringValid( ctx.font ) then
454-
Append( lines, "elPlayer.style.fontFamily = '%s';", ctx.font )
455-
end
456-
457-
local color = ctx.color
458-
local gradientColors = nil
472+
local colors = { ctx.color }
459473

474+
-- Get the player name color(s)
460475
if IsValid( value.ply ) then
461476
if CustomChat.USE_TAGS then
462477
local nameColor = CustomChat.Tags:GetNameColor( value.ply )
463-
if nameColor then color = nameColor end
478+
if nameColor then colors[1] = nameColor end
464479

465480
elseif value.ply.getChatTag then
466481
-- aTags support
467482
local _, _, nameColor = value.ply:getChatTag()
468-
if nameColor then color = nameColor end
483+
if nameColor then colors[1] = nameColor end
469484
end
470485

471486
local colorA, colorB = hook.Run( "OverrideCustomChatPlayerColor", value.ply )
472487

473488
if IsColor( colorA ) then
489+
colors[1] = colorA
490+
474491
if IsColor( colorB ) then
475-
gradientColors = { colorA, colorB }
476-
else
477-
color = colorA
492+
colors[2] = colorB
478493
end
479494
end
480495
end
481496

482-
if gradientColors then
483-
local colorGlow = Color(
484-
( gradientColors[1].r + gradientColors[2].r ) * 0.5,
485-
( gradientColors[1].g + gradientColors[2].g ) * 0.5,
486-
( gradientColors[1].b + gradientColors[2].b ) * 0.5
487-
)
497+
local lines = {}
488498

489-
Append( lines, "elPlayer.className = 'gradient';" )
490-
Append( lines, "elPlayer.style.textShadow = '0px 0px 0.2em %s';", ColorToRGB( colorGlow ) )
491-
Append( lines, "elPlayer.style.backgroundImage = '-webkit-linear-gradient(left, %s, %s)';",
492-
ColorToRGB( gradientColors[1] ), ColorToRGB( gradientColors[2] ) )
493-
end
499+
-- Create avatar image
500+
if not value.isBot and ctx.panel.displayAvatars then
501+
lines[#lines + 1] = Create.Image( CustomChat.FetchUserAvatarURL( value.id64, ctx.panel ), nil, "avatar ply-" .. value.id64 )
494502

495-
if color then
496-
if gradientColors == nil then
497-
Append( lines, "elPlayer.style.color = '%s';", ColorToRGB( color ) )
503+
if colors[1] then
504+
Append( lines, "elImg.style['border-color'] = '%s';", ColorToRGB( colors[1] ) )
498505
end
506+
end
507+
508+
local name = SafeString( value.name )
499509

500-
if ctx.panel.displayAvatars then
501-
Append( lines, "elImg.style['border-color'] = '%s';", ColorToRGB( color ) )
510+
if #colors > 1 then
511+
-- If we have more than one color, create a gradient
512+
lines[#lines + 1] = Create.Gradient( name, ctx.font, colors[1], colors[2], "elPlayer" )
513+
else
514+
-- Otherwise create a regular text element
515+
lines[#lines + 1] = Create.Element( "span", "elPlayer" )
516+
Append( lines, "elPlayer.textContent = '%s';", name )
517+
518+
if IsStringValid( ctx.font ) then
519+
Append( lines, "elPlayer.style.fontFamily = '%s';", ctx.font )
502520
end
503521
end
504522

523+
if not value.isBot then
524+
Append( lines, "elPlayer._playerData = '%s';", util.TableToJSON( {
525+
steamId = value.id,
526+
steamId64 = value.id64
527+
} ) )
528+
529+
Append( lines, "elPlayer.style.cursor = 'pointer';" )
530+
Append( lines, "elPlayer.clickableText = true;" )
531+
end
532+
505533
return table.concat( lines, "\n" )
506534
end
507535

@@ -612,24 +640,10 @@ blocks["gradient"] = function( value, ctx )
612640
local text = ChopEnds( string.match( value, "%([^%c]+%)" ), 2 )
613641

614642
components = string.Explode( ",", components, false )
643+
text = SafeString( text )
615644

616645
local colorA = Color( ParseComponent( components[1] ), ParseComponent( components[2] ), ParseComponent( components[3] ) )
617646
local colorB = Color( ParseComponent( components[4] ), ParseComponent( components[5] ), ParseComponent( components[6] ) )
618647

619-
local colorGlow = Color(
620-
( colorA.r + colorB.r ) * 0.5,
621-
( colorA.g + colorB.g ) * 0.5,
622-
( colorA.b + colorB.b ) * 0.5
623-
)
624-
625-
local code = [[%s
626-
elText.style.backgroundImage = '-webkit-linear-gradient(left, %s, %s)';
627-
elText.style.textShadow = '0px 0px 0.2em %s';]]
628-
629-
return code:format(
630-
Create.Text( text, ctx.font, nil, nil, nil, "gradient" ),
631-
ColorToRGB( colorA ),
632-
ColorToRGB( colorB ),
633-
ColorToRGB( colorGlow )
634-
)
648+
return Create.Gradient( text, ctx.font, colorA, colorB )
635649
end

lua/custom_chat/client/vgui/chat_history.lua

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,9 +343,24 @@ img {
343343
animation: ch_anim_rainbow 2s linear infinite;
344344
}
345345
346-
.gradient {
347-
background-image: -webkit-linear-gradient(left, #ff0000, #ff0088);
348-
background: linear-gradient(to left, #ff0000, #ff0088);
346+
.gradient-container {
347+
position: relative;
348+
}
349+
350+
.gradient-bg {
351+
color: blue;
352+
text-shadow: 0px 0px 0.15em blue;
353+
}
354+
355+
.gradient-fg {
356+
position: absolute;
357+
width: 100%;
358+
height: 100%;
359+
top: 0;
360+
left: 0;
361+
z-index: 10;
362+
363+
background-image: -webkit-linear-gradient(left, #ffffff, #ffffff);
349364
text-shadow: none;
350365
color: transparent;
351366

0 commit comments

Comments
 (0)