diff --git a/common/color_utils.py b/common/color_utils.py new file mode 100644 index 0000000000000000000000000000000000000000..a73b188779f59ecfcfe2d2438336e37da4a89ed6 --- /dev/null +++ b/common/color_utils.py @@ -0,0 +1,38 @@ +# -*- coding:utf-8 -*- +# title :颜色工具类 +# description :颜色工具类 +# author :Python超人 +# date :2023-02-11 +# link :https://gitcode.net/pythoncr/ +# python_version :3.8 +# ============================================================================== +from ursina import Vec4 + + +def to_vec4_color(colour: tuple, alpha=1) -> Vec4: + if len(colour) == 3: + return Vec4(colour[0], colour[1], colour[2], alpha * 255) / 255 + elif len(colour) == 4: + return Vec4(colour[0], colour[1], colour[2], colour[3]) / 255 + raise Exception("colour错误") + + +def adjust_brightness(color: Vec4, target_brightness: float = 0.6) -> Vec4: + """ + 调整颜色的亮度到目标 target_brightness(确保亮度不超过 1.0) + :param color: + :param target_brightness:(确保亮度不超过 1.0) + :return: + """ + # 获取颜色的亮度值 + brightness = color.x * 0.299 + color.y * 0.587 + color.z * 0.114 + + # 如果亮度值不够,增加亮度 + if brightness < target_brightness: + # 调整 RGB 值,确保亮度不超过 1.0 + r = min(color.x + (target_brightness - brightness), 1.0) + g = min(color.y + (target_brightness - brightness), 1.0) + b = min(color.z + (target_brightness - brightness), 1.0) + return Vec4(r, g, b, color.w) + else: + return color diff --git a/simulators/views/ursina_view.py b/simulators/views/ursina_view.py index 3a41d309dbf5de761df00887a5c1ae7cc078e5b9..96af54d61c6a0e8454dffe071b4f1750fc048f33 100644 --- a/simulators/views/ursina_view.py +++ b/simulators/views/ursina_view.py @@ -20,6 +20,7 @@ import os from bodies import Body import random +from common.color_utils import adjust_brightness, to_vec4_color from simulators.views.body_view import BodyView from simulators.views.ursina_mesh import create_sphere, create_torus, create_body_torus import numpy as np @@ -87,11 +88,11 @@ class Planet(Entity): self.trails = {} self.trail_len = 100 - b_color = self.body_view.body.color - trail_color = Vec4(b_color[0], b_color[1], b_color[2], 1) / 255 # 255 是原色,200会更亮一些 - self.trail_color = color.rgba(trail_color[0], trail_color[1], trail_color[2], 0.3) - self.trail_color *= 1.5 + # 根据天体的颜色获取拖尾的颜色 + trail_color = to_vec4_color(self.body_view.body.color) + trail_color = adjust_brightness(trail_color, 0.4) + self.trail_color = color.rgba(trail_color[0], trail_color[1], trail_color[2], 0.3) pos = body_view.position * body_view.body.distance_scale * SCALE_FACTOR scale = body_view.body.diameter * body_view.body.size_scale * SCALE_FACTOR