提交 8c71a776 编写于 作者: 三月三net's avatar 三月三net

太阳系三体模拟器

上级 79a573a3
# -*- 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
...@@ -20,6 +20,7 @@ import os ...@@ -20,6 +20,7 @@ import os
from bodies import Body from bodies import Body
import random import random
from common.color_utils import adjust_brightness, to_vec4_color
from simulators.views.body_view import BodyView from simulators.views.body_view import BodyView
from simulators.views.ursina_mesh import create_sphere, create_torus, create_body_torus from simulators.views.ursina_mesh import create_sphere, create_torus, create_body_torus
import numpy as np import numpy as np
...@@ -87,11 +88,11 @@ class Planet(Entity): ...@@ -87,11 +88,11 @@ class Planet(Entity):
self.trails = {} self.trails = {}
self.trail_len = 100 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 pos = body_view.position * body_view.body.distance_scale * SCALE_FACTOR
scale = body_view.body.diameter * body_view.body.size_scale * SCALE_FACTOR scale = body_view.body.diameter * body_view.body.size_scale * SCALE_FACTOR
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册