def to_binary(self): """ struct rgb { u8 r, g, b; } struct data { u8 pixel_format; u8 entry_bits; u16 lut_entry_count; rgb lut[lut_entry_count] }; :return: binary """ bins = struct.pack('<BBH', self._pixel_format.value, self.entry_bits, len(self._lut)) for i in range(len(self._lut)): r, g, b = ImageUtil.rgb(self._lut[i]) bins += struct.pack('<BBB', r, g, b) while len(bins) % 4 != 0: bins += b'\x00' return bins
def _fill_rect(self, window_line_buf, window, y, block_x): if self._gradient_mode == GradientMode.NONE: return width = window.width if self._width == 0 else self._width height = window.height if self._height == 0 else self._height bg_color_start = self.get_color(self._bgcolor_start) bg_color_end = self.get_color(self._bgcolor_end) color_steps = 1 fill_width = width - self._border_weight * 2 fill_height = height - self._border_weight * 2 if self._gradient_mode == GradientMode.LEFT_TO_RIGHT: color_steps = fill_width elif self._gradient_mode == GradientMode.TOP_TO_BOTTOM: color_steps = fill_height elif self._gradient_mode == GradientMode.TOP_LEFT_TO_BOTTOM_RIGHT or \ self._gradient_mode == GradientMode.BOTTOM_LEFT_TO_TOP_RIGHT: color_steps = fill_width * fill_height elif self._gradient_mode == GradientMode.CORNER_TO_CENTER: color_steps = (fill_width * fill_height) >> 2 r_start, g_start, b_start = ImageUtil.rgb(bg_color_start) r_end, g_end, b_end = ImageUtil.rgb(bg_color_end) r_delta = (r_end - r_start) / color_steps g_delta = (g_end - g_start) / color_steps b_delta = (b_end - b_start) / color_steps for x in range(block_x + self._border_weight, block_x + width - self._border_weight): fill_x = x - (block_x + self._border_weight) fill_y = y - self._border_weight if self._gradient_mode == GradientMode.SOLID: factor = 0 elif self._gradient_mode == GradientMode.LEFT_TO_RIGHT: factor = fill_x elif self._gradient_mode == GradientMode.TOP_TO_BOTTOM: factor = y elif self._gradient_mode == GradientMode.TOP_LEFT_TO_BOTTOM_RIGHT: factor = fill_x * fill_y elif self._gradient_mode == GradientMode.BOTTOM_LEFT_TO_TOP_RIGHT: factor = fill_x * (fill_height - fill_y) elif self._gradient_mode == GradientMode.CORNER_TO_CENTER: if fill_x <= (fill_width >> 1): factor_x = fill_x else: factor_x = fill_width - fill_x if fill_y <= (fill_height >> 1): factor_y = fill_y else: factor_y = (fill_height - fill_y) factor = factor_x * factor_y else: raise Exception('Unknown gradient mode <%s>' % self._gradient_mode.name) color = ImageUtil.color_add(bg_color_start, r_delta * factor, g_delta * factor, b_delta * factor) if 0 <= x < window.width: window_line_buf[x] = color