11# Copyright (C) Code Partners Pty. Ltd. All rights reserved. #
2+ import typing
23from typing import Optional
34
5+ from common .color .rgbacolor import RGBAColor
46from common .file_rotate import FileRotate
57from common .level import Level
68from common .color .color import Color
@@ -27,6 +29,22 @@ class LookupTable:
2729 __KB_FACTOR = 1024
2830 __MB_FACTOR = __KB_FACTOR * 1024
2931 __GB_FACTOR = __MB_FACTOR * 1024
32+ __HEX_ID = (
33+ "0x" , # C#, Java and Python
34+ "&H" , # Visual Basic .NET
35+ "$" , # Object Pascal
36+ )
37+ __HEX_TBL = (
38+ 0x7f , 0x7f , 0x7f , 0x7f , 0x7f , 0x7f , 0x7f , 0x7f ,
39+ 0x7f , 0x7f , 0x7f , 0x7f , 0x7f , 0x7f , 0x7f , 0x7f ,
40+ 0x7f , 0x7f , 0x7f , 0x7f , 0x7f , 0x7f , 0x7f , 0x7f ,
41+ 0x7f , 0x7f , 0x7f , 0x7f , 0x7f , 0x7f , 0x7f , 0x7f ,
42+ 0x7f , 0x7f , 0x7f , 0x7f , 0x7f , 0x7f , 0x7f , 0x7f ,
43+ 0x7f , 0x7f , 0x7f , 0x7f , 0x7f , 0x7f , 0x7f , 0x7f ,
44+ 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 ,
45+ 0x08 , 0x09 , 0x7f , 0x7f , 0x7f , 0x7f , 0x7f , 0x7f ,
46+ 0x7f , 0x0a , 0x0b , 0x0c , 0x0d , 0x0e , 0x0f , 0x7f
47+ )
3048
3149 def __init__ (self ):
3250 """
@@ -193,10 +211,10 @@ def __is_valid_integer(cls, value: str) -> bool:
193211 pass
194212 return False
195213
196- def get_color_value (self , key : str , default_value : Color ) -> Color :
214+ def get_color_value (self , key : str , default_value : ( Color , RGBAColor )) -> ( RGBAColor , Color ) :
197215 """
198- Returns a Color value of an element for a given key.
199- Returns either the value converted to a Color value for the given key
216+ Returns a Color/RGBAColor value of an element for a given key.
217+ Returns either the value converted to a Color/RGBAColor value for the given key
200218 if an element with the given key exists and the found value has a valid format or default_value otherwise.
201219
202220 The element value must be specified as a hexadecimal string. The hexadecimal value must represent
@@ -217,17 +235,31 @@ def get_color_value(self, key: str, default_value: Color) -> Color:
217235 found value has an invalid format.
218236 :raises: TypeError if the default_value is not Color type.
219237 """
220- if not isinstance (default_value , Color ):
221- raise TypeError ("default_value must be a Color" )
238+ if not ( isinstance (default_value , Color ) or isinstance ( default_value , RGBAColor ) ):
239+ raise TypeError ("default_value must be a Color or RGBAColor " )
222240
223241 value = self .get_string_value (key , "" )
224242
225243 if not value :
226244 return default_value
227-
228- @staticmethod
229- def __convert_hex_value (value : str ):
230- ...
245+ else :
246+ b = self .__convert_hex_value (value .strip ())
247+ if len (b ) == 3 :
248+ return RGBAColor (0xff & b [0 ],
249+ 0xff & b [1 ],
250+ 0xff & b [2 ])
251+ elif len (b ) == 4 :
252+ return RGBAColor (0xff & b [0 ],
253+ 0xff & b [0 ],
254+ 0xff & b [0 ],
255+ 0xff & b [0 ])
256+
257+ def __convert_hex_value (self , value : str ) -> typing .Optional [bytearray ]:
258+ for prefix in self .__HEX_ID :
259+ if value .startswith (prefix ):
260+ value = value [len (prefix ):]
261+ return self .__convert_hex_string (value )
262+ return None
231263
232264 def get_boolean_value (self , key : str , default_value : bool ) -> bool :
233265 """
@@ -453,3 +485,20 @@ def __convert_unicode_value(value: str) -> Optional[bytes]:
453485 return value .encode ('utf-8' )
454486 except UnicodeEncodeError :
455487 return None
488+
489+ def __convert_hex_string (self , value : str ) -> typing .Optional [bytes ]:
490+ value = value .upper ()
491+ if len (value ) % 2 != 0 : # Check if length is odd
492+ value += "0" # Append a '0' nibble
493+
494+ b = None
495+ if self .__is_valid_hex (value ):
496+ b = bytes .fromhex (value )
497+ return b
498+
499+ def __is_valid_hex (self , value : str ) -> bool :
500+ for char in value :
501+ code_point = ord (char )
502+ if code_point >= len (self .__HEX_TBL ) or self .__HEX_TBL [code_point ] > 0x0f :
503+ return False
504+ return True
0 commit comments