- Layout improvements for the Chat client
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import json
|
||||
import re
|
||||
|
||||
"""
|
||||
Utility functions for chat customization.
|
||||
@@ -20,18 +21,20 @@ def get_default_chat_customisation(tenant_customisation=None):
|
||||
"""
|
||||
# Default customization options
|
||||
default_customisation = {
|
||||
'primary_color': '#007bff',
|
||||
'secondary_color': '#6c757d',
|
||||
'background_color': '#ffffff',
|
||||
'text_color': '#212529',
|
||||
'sidebar_markdown': '',
|
||||
'sidebar_color': '#f8f9fa',
|
||||
'sidebar_background': '#2c3e50',
|
||||
'gradient_start_color': '#f5f7fa',
|
||||
'gradient_end_color': '#c3cfe2',
|
||||
'markdown_background_color': 'transparent',
|
||||
'markdown_text_color': '#ffffff',
|
||||
'sidebar_markdown': '',
|
||||
'progress_tracker_insights': 'No Information'
|
||||
'gradient_start_color': '#f5f7fa',
|
||||
'gradient_end_color': '#c3cfe2',
|
||||
'progress_tracker_insights': 'No Information',
|
||||
'active_background_color': '#ffffff',
|
||||
'active_text_color': '#212529',
|
||||
'history_background': 10,
|
||||
'history_user_message_background': -10,
|
||||
'history_ai_message_background': 0,
|
||||
'history_message_text_color': '#212529',
|
||||
}
|
||||
|
||||
# If no tenant customization is provided, return the defaults
|
||||
@@ -56,3 +59,112 @@ def get_default_chat_customisation(tenant_customisation=None):
|
||||
customisation[key] = value
|
||||
|
||||
return customisation
|
||||
|
||||
|
||||
def hex_to_rgb(hex_color):
|
||||
"""
|
||||
Convert hex color to RGB tuple.
|
||||
|
||||
Args:
|
||||
hex_color (str): Hex color string (e.g., '#ffffff' or 'ffffff')
|
||||
|
||||
Returns:
|
||||
tuple: RGB values as (r, g, b)
|
||||
"""
|
||||
# Remove # if present
|
||||
hex_color = hex_color.lstrip('#')
|
||||
|
||||
# Handle 3-character hex codes
|
||||
if len(hex_color) == 3:
|
||||
hex_color = ''.join([c*2 for c in hex_color])
|
||||
|
||||
# Convert to RGB
|
||||
try:
|
||||
return tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4))
|
||||
except ValueError:
|
||||
# Return white as fallback
|
||||
return (255, 255, 255)
|
||||
|
||||
|
||||
def adjust_color_alpha(percentage):
|
||||
"""
|
||||
Convert percentage to RGBA color with appropriate base color and alpha.
|
||||
|
||||
Args:
|
||||
percentage (int): Percentage (-50 to 50)
|
||||
Positive = white base (lighten)
|
||||
Negative = black base (darken)
|
||||
Zero = transparent
|
||||
|
||||
Returns:
|
||||
str: RGBA color string for CSS
|
||||
"""
|
||||
if percentage == 0:
|
||||
return 'rgba(255, 255, 255, 0)' # Volledig transparant
|
||||
|
||||
# Bepaal basis kleur
|
||||
if percentage > 0:
|
||||
# Positief = wit voor verheldering
|
||||
base_color = (255, 255, 255)
|
||||
else:
|
||||
# Negatief = zwart voor verdonkering
|
||||
base_color = (0, 0, 0)
|
||||
|
||||
# Bereken alpha op basis van percentage (max 50 = alpha 1.0)
|
||||
alpha = abs(percentage) / 50.0
|
||||
alpha = max(0.0, min(1.0, alpha)) # Zorg voor 0.0-1.0 range
|
||||
|
||||
return f'rgba({base_color[0]}, {base_color[1]}, {base_color[2]}, {alpha})'
|
||||
|
||||
|
||||
def adjust_color_brightness(hex_color, percentage):
|
||||
"""
|
||||
Adjust the brightness of a hex color by a percentage.
|
||||
|
||||
Args:
|
||||
hex_color (str): Hex color string (e.g., '#ffffff')
|
||||
percentage (int): Percentage to adjust (-100 to 100)
|
||||
Positive = lighter, Negative = darker
|
||||
|
||||
Returns:
|
||||
str: RGBA color string for CSS (e.g., 'rgba(255, 255, 255, 0.9)')
|
||||
"""
|
||||
if not hex_color or not isinstance(hex_color, str):
|
||||
return 'rgba(255, 255, 255, 0.1)'
|
||||
|
||||
# Get RGB values
|
||||
r, g, b = hex_to_rgb(hex_color)
|
||||
|
||||
# Calculate adjustment factor
|
||||
if percentage > 0:
|
||||
# Lighten: move towards white
|
||||
factor = percentage / 100.0
|
||||
r = int(r + (255 - r) * factor)
|
||||
g = int(g + (255 - g) * factor)
|
||||
b = int(b + (255 - b) * factor)
|
||||
else:
|
||||
# Darken: move towards black
|
||||
factor = abs(percentage) / 100.0
|
||||
r = int(r * (1 - factor))
|
||||
g = int(g * (1 - factor))
|
||||
b = int(b * (1 - factor))
|
||||
|
||||
# Ensure values are within 0-255 range
|
||||
r = max(0, min(255, r))
|
||||
g = max(0, min(255, g))
|
||||
b = max(0, min(255, b))
|
||||
|
||||
# Return as rgba with slight transparency for better blending
|
||||
return f'rgba({r}, {g}, {b}, 0.9)'
|
||||
|
||||
|
||||
def get_base_background_color():
|
||||
"""
|
||||
Get the base background color for history adjustments.
|
||||
This should be the main chat background color.
|
||||
|
||||
Returns:
|
||||
str: Hex color string
|
||||
"""
|
||||
# Use a neutral base color that works well with adjustments
|
||||
return '#f8f9fa'
|
||||
|
||||
@@ -5,6 +5,7 @@ import markdown
|
||||
from markupsafe import Markup
|
||||
from datetime import datetime
|
||||
from common.utils.nginx_utils import prefixed_url_for as puf
|
||||
from common.utils.chat_utils import adjust_color_brightness, adjust_color_alpha, get_base_background_color
|
||||
from flask import current_app, url_for
|
||||
|
||||
|
||||
@@ -116,7 +117,10 @@ def register_filters(app):
|
||||
app.jinja_env.filters['prefixed_url_for'] = prefixed_url_for
|
||||
app.jinja_env.filters['markdown'] = render_markdown
|
||||
app.jinja_env.filters['clean_markdown'] = clean_markdown
|
||||
app.jinja_env.filters['adjust_color_brightness'] = adjust_color_brightness
|
||||
app.jinja_env.filters['adjust_color_alpha'] = adjust_color_alpha
|
||||
|
||||
app.jinja_env.globals['prefixed_url_for'] = prefixed_url_for
|
||||
app.jinja_env.globals['get_pagination_html'] = get_pagination_html
|
||||
app.jinja_env.globals['get_base_background_color'] = get_base_background_color
|
||||
|
||||
|
||||
Reference in New Issue
Block a user