Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli/lib): use scene background color as default #160

Merged
merged 8 commits into from
Mar 22, 2023
2 changes: 2 additions & 0 deletions manim_slides/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from typing import Dict, List, Optional, Set, Tuple, Union

from pydantic import BaseModel, FilePath, PositiveInt, root_validator, validator
from pydantic.color import Color
from PySide6.QtCore import Qt

from .defaults import FFMPEG_BIN
Expand Down Expand Up @@ -150,6 +151,7 @@ class PresentationConfig(BaseModel): # type: ignore
slides: List[SlideConfig]
files: List[FilePath]
resolution: Tuple[PositiveInt, PositiveInt] = (1920, 1080)
background_color: Color = "black"

@root_validator
def animation_indices_match_files(
Expand Down
4 changes: 2 additions & 2 deletions manim_slides/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,9 @@ def get_sections_iter(self, assets_dir: Path) -> Generator[str, None, None]:
# Read more about this:
# https://developer.mozilla.org/en-US/docs/Web/Media/Autoplay_guide#autoplay_and_autoplay_blocking
if slide_config.is_loop():
yield f'<section data-background-size={self.background_size.value} data-background-video="{file}" data-background-video-muted data-background-video-loop></section>'
yield f'<section data-background-size={self.background_size.value} data-background-color="{presentation_config.background_color}" data-background-video="{file}" data-background-video-muted data-background-video-loop></section>'
else:
yield f'<section data-background-size={self.background_size.value} data-background-video="{file}" data-background-video-muted></section>'
yield f'<section data-background-size={self.background_size.value} data-background-color="{presentation_config.background_color}" data-background-video="{file}" data-background-video-muted></section>'

def load_template(self) -> str:
"""Returns the RevealJS HTML template as a string."""
Expand Down
29 changes: 24 additions & 5 deletions manim_slides/present.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import numpy as np
from click import Context, Parameter
from pydantic import ValidationError
from pydantic.color import Color
from PySide6.QtCore import Qt, QThread, Signal, Slot
from PySide6.QtGui import QCloseEvent, QIcon, QImage, QKeyEvent, QPixmap, QResizeEvent
from PySide6.QtWidgets import QApplication, QGridLayout, QLabel, QWidget
Expand Down Expand Up @@ -105,6 +106,11 @@ def resolution(self) -> Tuple[int, int]:
"""Returns the resolution."""
return self.config.resolution

@property
def background_color(self) -> Color:
"""Returns the background color."""
return self.config.background_color

@property
def current_slide_index(self) -> int:
return self.__current_slide_index
Expand Down Expand Up @@ -411,6 +417,11 @@ def current_resolution(self) -> Tuple[int, int]:
"""Returns the resolution of the current presentation."""
return self.current_presentation.resolution

@property
def current_background_color(self) -> Color:
"""Returns the background color of the current presentation."""
return self.current_presentation.background_color

def run(self) -> None:
"""Runs a series of presentations until end or exit."""
while self.run_flag:
Expand Down Expand Up @@ -649,7 +660,9 @@ def __init__(
self.label.setScaledContents(True)
self.label.setAlignment(Qt.AlignCenter)
self.label.resize(self.display_width, self.display_height)
self.label.setStyleSheet(f"background-color: {background_color}")
self.label.setStyleSheet(
f"background-color: {self.thread.current_background_color}"
)

self.pixmap = QPixmap(self.width(), self.height())
self.label.setPixmap(self.pixmap)
Expand Down Expand Up @@ -729,6 +742,9 @@ def update_canvas(self) -> None:
self.display_width, self.display_height = self.thread.current_resolution
if not self.isFullScreen():
self.resize(self.display_width, self.display_height)
self.label.setStyleSheet(
f"background-color: {self.thread.current_background_color}"
)


@click.command()
Expand Down Expand Up @@ -924,8 +940,8 @@ def str_to_int_or_none(value: str) -> Optional[int]:
"background_color",
metavar="COLOR",
type=str,
default="black",
help='Set the background color for borders when using "keep" resize mode. Can be any valid CSS color, e.g., "green", "#FF6500" or "rgba(255, 255, 0, .5)".',
default=None,
help='Set the background color for borders when using "keep" resize mode. Can be any valid CSS color, e.g., "green", "#FF6500" or "rgba(255, 255, 0, .5)". If not set, it defaults to the background color configured in the Manim scene.',
show_default=True,
)
@click.option(
Expand Down Expand Up @@ -980,7 +996,7 @@ def present(
hide_mouse: bool,
aspect_ratio: str,
resize_mode: str,
background_color: str,
background_color: Optional[str],
start_at: Tuple[Optional[int], Optional[int], Optional[int]],
start_at_scene_number: Optional[int],
start_at_slide_number: Optional[int],
Expand All @@ -1005,6 +1021,10 @@ def present(
for presentation_config in presentation_configs:
presentation_config.resolution = resolution

if background_color is not None:
for presentation_config in presentation_configs:
presentation_config.background_color = background_color

Comment on lines +1024 to +1027
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you need to remove the background_color option from App :-)

presentations = [
Presentation(presentation_config)
for presentation_config in presentation_configs
Expand Down Expand Up @@ -1048,7 +1068,6 @@ def present(
hide_mouse=hide_mouse,
aspect_ratio=ASPECT_RATIO_MODES[aspect_ratio],
resize_mode=RESIZE_MODES[resize_mode],
background_color=background_color,
start_at_scene_number=start_at_scene_number,
start_at_slide_number=start_at_slide_number,
start_at_animation_number=start_at_animation_number,
Expand Down
13 changes: 12 additions & 1 deletion manim_slides/slide.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ def __init__(
self.__loop_start_animation: Optional[int] = None
self.__pause_start_animation = 0

@property
def __background_color(self) -> Tuple[int, int]:
Fairlight8 marked this conversation as resolved.
Show resolved Hide resolved
"""Returns the scene's background color."""
if MANIMGL:
return self.camera_config["background_color"]
else:
return config["background_color"]
Fairlight8 marked this conversation as resolved.
Show resolved Hide resolved

@property
def __resolution(self) -> Tuple[int, int]:
"""Returns the scene's resolution used during rendering."""
Expand Down Expand Up @@ -321,7 +329,10 @@ def __save_slides(self, use_cache: bool = True) -> None:
with open(slide_path, "w") as f:
f.write(
PresentationConfig(
slides=self.__slides, files=files, resolution=self.__resolution
slides=self.__slides,
files=files,
resolution=self.__resolution,
background_color=self.__background_color.hex,
Fairlight8 marked this conversation as resolved.
Show resolved Hide resolved
).json(indent=2)
)

Expand Down