From f33166d20c2e6329cddfaaf5bd9582292a9137e4 Mon Sep 17 00:00:00 2001
From: Preksha Chouhan <prec@odoo.com>
Date: Fri, 24 Mar 2023 06:45:41 +0000
Subject: [PATCH] [FIX] web_editor: convert color opacity to be compatible with
 PIL Image color

ValueError 'unknown colour specifier' occurs when we access
export_icon_to_png(). This error occurs when we change the colour of an
icon in the mailing template, because the value of alpha (opacity) in 'rgba'
is in the range of 0 to 1, but PIL Image support colour opacity range 0 to 255.

This commit converts the opacity value range (0 to 1) to a range (0 to 255)
compatible with the PIL image library when the colour specifier is 'rgba'.

sentry - 3933353285

closes odoo/odoo#121516

X-original-commit: a541877045d262ffeac58a4103dc2bbdcc13c10b
Signed-off-by: David Monjoie (dmo) <dmo@odoo.com>
---
 addons/web_editor/controllers/main.py | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/addons/web_editor/controllers/main.py b/addons/web_editor/controllers/main.py
index d373ad85b4e2..f7ab358d4465 100644
--- a/addons/web_editor/controllers/main.py
+++ b/addons/web_editor/controllers/main.py
@@ -11,6 +11,7 @@ import werkzeug.urls
 from PIL import Image, ImageFont, ImageDraw
 from lxml import etree
 from base64 import b64decode, b64encode
+from math import floor
 
 from odoo.http import request, Response
 from odoo import http, tools, _, SUPERUSER_ID
@@ -110,6 +111,13 @@ class Web_Editor(http.Controller):
             bg = bg.replace('rgba', 'rgb')
             bg = ','.join(bg.split(',')[:-1])+')'
 
+        # Convert the opacity value compatible with PIL Image color (0 to 255)
+        # when color specifier is 'rgba'
+        if color is not None and color.startswith('rgba'):
+            *rgb, a = color.strip(')').split(',')
+            opacity = str(floor(float(a) * 255))
+            color = ','.join([*rgb, opacity]) + ')'
+
         # Determine the dimensions of the icon
         image = Image.new("RGBA", (width, height), color)
         draw = ImageDraw.Draw(image)
-- 
GitLab