1+ # cython: language_level=3
2+
13from colorsys import hsv_to_rgb as _colorsys__hsv_to_rgb
24from colorsys import rgb_to_hsv as _colorsys__rgb_to_hsv
35
@@ -126,106 +128,102 @@ cdef class Color:
126128
127129cdef class DisplayScalar:
128130 """
129- 🟩 **R** -
131+ 🟩 **R** - Optimized version
130132 """
131133 cdef double _point
132134 cdef object _logger
133- cdef int display_height
135+ cdef double display_height
136+ cdef object _display_object # Store reference to avoid repeated lookups
134137
135138 def __init__ (self ):
136139 """
137- 🟩 **R** -
140+ 🟩 **R** - Optimized
138141 """
139142 self ._point = 0.0
140143 self ._logger = _InternalLogger()
141- self .display_height = _Registry.pmma_module_spine[Constants.DISPLAY_OBJECT].get_height()
144+ self ._display_object = _Registry.pmma_module_spine[Constants.DISPLAY_OBJECT]
145+ self .display_height = self ._display_object.get_height()
142146
143- cpdef void update_display_height(self ):
144- self .display_height = _Registry.pmma_module_spine[Constants.DISPLAY_OBJECT].get_height()
147+ cpdef void update_display_height(self ) noexcept:
148+ """ Update display height with stored reference """
149+ self .display_height = self ._display_object.get_height()
145150
146- cpdef void set_point(self , double value, str in_type = Constants.CONVENTIONAL_COORDINATES) :
151+ cpdef void set_point(self , double value, str in_type) noexcept :
147152 """
148- 🟩 **R** -
153+ 🟩 **R** - Optimized
149154 """
150155 cdef double half_display_height
151156
152- if in_type == Constants.CONVENTIONAL_COORDINATES:
157+ if in_type == Constants.CONVENTIONAL_COORDINATES: # Constants.CONVENTIONAL_COORDINATES
153158 self ._point = value
154- elif in_type == Constants.OPENGL_COORDINATES:
155- half_display_height = self .display_height / 2.0
159+ elif in_type == Constants.OPENGL_COORDINATES: # Constants.OPENGL_COORDINATES
160+ half_display_height = self .display_height * 0.5
156161 self ._point = value * half_display_height
157162
158- cpdef double get_point(self , str out_type = Constants.CONVENTIONAL_COORDINATES) :
163+ cpdef double get_point(self , str out_type) noexcept :
159164 """
160- 🟩 **R** -
165+ 🟩 **R** - Optimized
161166 """
162- if out_type == Constants.CONVENTIONAL_COORDINATES:
167+ if out_type == Constants.CONVENTIONAL_COORDINATES: # Constants.CONVENTIONAL_COORDINATES
163168 return self ._point
164- elif out_type == Constants.OPENGL_COORDINATES:
165- return self ._point / (self .display_height / 2.0 )
169+ elif out_type == Constants.OPENGL_COORDINATES: # Constants.OPENGL_COORDINATES
170+ return self ._point / (self .display_height * 0.5 )
166171
167172cdef class DisplayCoordinates:
168173 """
169- 🟩 **R** -
174+ 🟩 **R** - Optimized
170175 """
171- cdef list _coordinate
176+ cdef double _coordinate[ 2 ] # Use a fixed-size C array
172177 cdef object _logger
173- cdef tuple display_size
178+ cdef double display_width, display_height
179+ cdef object _display_object # Store reference for performance
174180
175181 def __init__ (self ):
176182 """
177- 🟩 **R** -
183+ 🟩 **R** - Optimized
178184 """
179- self ._coordinate = [0.0 , 0.0 ]
185+ self ._coordinate[0 ] = 0.0
186+ self ._coordinate[1 ] = 0.0
180187 self ._logger = _InternalLogger()
188+ self ._display_object = _Registry.pmma_module_spine[Constants.DISPLAY_OBJECT]
189+ self .display_width, self .display_height = self ._display_object.get_size()
181190
182- self .display_size = _Registry.pmma_module_spine[Constants.DISPLAY_OBJECT].get_size()
183-
184- cpdef void update_display_size(self ):
185- self .display_size = _Registry.pmma_module_spine[Constants.DISPLAY_OBJECT].get_size()
191+ cpdef void update_display_size(self ) noexcept:
192+ """ Update display size """
193+ self .display_width, self .display_height = self ._display_object.get_size()
186194
187- cpdef void set_coordinate(self , object coordinate, str in_type = Constants.CONVENTIONAL_COORDINATES) :
195+ cpdef void set_coordinate(self , object coordinate, str in_type) noexcept :
188196 """
189- 🟩 **R** -
197+ 🟩 **R** - Optimized
190198 """
191- cdef list converted_coordinate
199+ cdef double x, y
192200
193- if isinstance (coordinate, (list , tuple , _numpy.ndarray)):
194- converted_coordinate = list (coordinate)
195- else :
196- converted_coordinate = [float (coordinate)]
197-
198- if len (converted_coordinate) == 0 :
199- converted_coordinate = [0.0 , 0.0 ]
200- elif len (converted_coordinate) == 1 :
201- converted_coordinate.append(0.0 )
202- elif len (converted_coordinate) > 2 :
203- self ._logger.log_development(" This process is only required for 2D coordinates." )
204- converted_coordinate = converted_coordinate[:2 ]
201+ try :
202+ # Assume coordinate is iterable and extract values
203+ x, y = coordinate[0 ], coordinate[1 ]
204+ except (IndexError , TypeError ):
205+ x, y = 0.0 , 0.0 # Default case
205206
206- cdef double half_display_width, half_display_height, x, y
207+ cdef double half_display_width = self .display_width * 0.5
208+ cdef double half_display_height = self .display_height * 0.5
207209
208- if in_type == Constants.CONVENTIONAL_COORDINATES:
209- self ._coordinate = converted_coordinate
210- elif in_type == Constants.OPENGL_COORDINATES:
211- half_display_width = self .display_size[0 ] / 2.0
212- half_display_height = self .display_size[1 ] / 2.0
213- x = half_display_width * (converted_coordinate[0 ] + 1 )
214- y = - half_display_height * (converted_coordinate[1 ] - 1 )
215- self ._coordinate = [x, y]
210+ if in_type == Constants.CONVENTIONAL_COORDINATES: # Constants.CONVENTIONAL_COORDINATES
211+ self ._coordinate[0 ] = x
212+ self ._coordinate[1 ] = y
213+ elif in_type == Constants.OPENGL_COORDINATES: # Constants.OPENGL_COORDINATES
214+ self ._coordinate[0 ] = half_display_width * (x + 1 )
215+ self ._coordinate[1 ] = - half_display_height * (y - 1 )
216216
217- cpdef cnp.ndarray[cnp.float32_t, ndim= 1 ] get_coordinate(self , str out_type = Constants.CONVENTIONAL_COORDINATES ):
217+ cpdef cnp.ndarray[cnp.float32_t, ndim= 1 ] get_coordinate(self , str out_type):
218218 """
219- 🟩 **R** -
219+ 🟩 **R** - Optimized
220220 """
221- cdef double display_width, display_height, x, y
221+ cdef double x, y
222222
223- if out_type == Constants.CONVENTIONAL_COORDINATES:
224- return _numpy.array(self ._coordinate, dtype = _numpy.float32)
223+ if out_type == Constants.CONVENTIONAL_COORDINATES: # Constants.CONVENTIONAL_COORDINATES
224+ return _numpy.array([ self ._coordinate[ 0 ], self ._coordinate[ 1 ]] , dtype = _numpy.float32)
225225
226- elif out_type == Constants.OPENGL_COORDINATES:
227- display_width = self .display_size[0 ]
228- display_height = self .display_size[1 ]
229- x = (2.0 * self ._coordinate[0 ]) / display_width - 1.0
230- y = 1.0 - (2.0 * self ._coordinate[1 ]) / display_height
231- return _numpy.array([x, y], dtype = _numpy.float32)
226+ elif out_type == Constants.OPENGL_COORDINATES: # Constants.OPENGL_COORDINATES
227+ x = (2.0 * self ._coordinate[0 ]) / self .display_width - 1.0
228+ y = 1.0 - (2.0 * self ._coordinate[1 ]) / self .display_height
229+ return _numpy.array([x, y], dtype = _numpy.float32)
0 commit comments