From 8c71a7761bdff486b90aba9dbd5d2ede651f2d87 Mon Sep 17 00:00:00 2001 From: march3 Date: Thu, 16 Mar 2023 11:06:16 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=AA=E9=98=B3=E7=B3=BB=E4=B8=89=E4=BD=93?= =?UTF-8?q?=E6=A8=A1=E6=8B=9F=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/color_utils.py | 38 +++++++++++++++++++++++++++++++++ simulators/views/ursina_view.py | 9 ++++---- 2 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 common/color_utils.py diff --git a/common/color_utils.py b/common/color_utils.py new file mode 100644 index 0000000..a73b188 --- /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 3a41d30..96af54d 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 -- GitLab