Skip to content

Commit c76f192

Browse files
committed
♻️ refactor: rustify writer utility convert_color
1 parent 316541c commit c76f192

File tree

7 files changed

+55
-41
lines changed

7 files changed

+55
-41
lines changed

packages/biliass/rust/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ edition = "2021"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77
[lib]
8-
name = "docstring_parser"
8+
name = "biliass_core"
99
crate-type = ["cdylib"]
1010

1111
[dependencies]

packages/biliass/rust/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@ fn biliass_pyo3(m: &Bound<'_, PyModule>) -> PyResult<()> {
2727
m.add_function(wrap_pyfunction!(python::py_read_comments_from_protobuf, m)?)?;
2828
m.add_function(wrap_pyfunction!(python::py_convert_timestamp, m)?)?;
2929
m.add_function(wrap_pyfunction!(python::py_ass_escape, m)?)?;
30+
m.add_function(wrap_pyfunction!(python::py_convert_color, m)?)?;
3031
Ok(())
3132
}

packages/biliass/rust/src/python/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ mod writer;
66
pub use comment::{PyComment, PyCommentPosition};
77
pub use proto::{PyDanmakuElem, PyDmSegMobileReply};
88
pub use reader::{py_read_comments_from_protobuf, py_read_comments_from_xml};
9-
pub use writer::{py_ass_escape, py_convert_timestamp};
9+
pub use writer::{py_ass_escape, py_convert_color, py_convert_timestamp};

packages/biliass/rust/src/python/writer.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,9 @@ pub fn py_convert_timestamp(timestamp: f64) -> PyResult<String> {
1111
pub fn py_ass_escape(text: &str) -> PyResult<String> {
1212
Ok(writer::utils::ass_escape(text))
1313
}
14+
15+
#[pyfunction(name = "convert_color")]
16+
#[pyo3(signature = (rgb, width = 1280, height = 576))]
17+
pub fn py_convert_color(rgb: u32, width: u32, height: u32) -> PyResult<String> {
18+
Ok(writer::utils::convert_color(rgb, Some(width), Some(height)))
19+
}

packages/biliass/rust/src/writer/utils.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,47 @@ pub fn ass_escape(text: &str) -> String {
3939
.collect::<Vec<_>>()
4040
.join("\\N")
4141
}
42+
43+
pub fn convert_color(rgb: u32, width: Option<u32>, height: Option<u32>) -> String {
44+
let width = width.unwrap_or(1280);
45+
let height = height.unwrap_or(576);
46+
if rgb == 0x000000 {
47+
return "000000".to_owned();
48+
} else if rgb == 0xFFFFFF {
49+
return "FFFFFF".to_owned();
50+
}
51+
let r = (rgb >> 16) & 0xFF;
52+
let g = (rgb >> 8) & 0xFF;
53+
let b = rgb & 0xFF;
54+
if width < 1280 && height < 576 {
55+
format!("{:02X}{:02X}{:02X}", b, g, r)
56+
} else {
57+
let clip_byte = |x: f64| -> u8 {
58+
if x > 255.0 {
59+
255
60+
} else if x < 0.0 {
61+
0
62+
} else {
63+
x.round() as u8
64+
}
65+
};
66+
format!(
67+
"{:02X}{:02X}{:02X}",
68+
clip_byte(
69+
r as f64 * 0.009_563_840_880_806_56
70+
+ g as f64 * 0.032_172_545_402_037_29
71+
+ b as f64 * 0.958_263_613_717_156_1
72+
),
73+
clip_byte(
74+
r as f64 * -0.104_939_331_420_753_9
75+
+ g as f64 * 1.172_314_781_918_551_5
76+
+ b as f64 * -0.067_375_450_497_797_57
77+
),
78+
clip_byte(
79+
r as f64 * 0.913_489_123_739_876_5
80+
+ g as f64 * 0.078_585_363_725_325_1
81+
+ b as f64 * 0.007_925_512_534_798_42
82+
),
83+
)
84+
}
85+
}

packages/biliass/src/biliass/_core.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,4 @@ def read_comments_from_xml(text: str, fontsize: float) -> list[Comment]: ...
5959
def read_comments_from_protobuf(data: bytes, fontsize: float) -> list[Comment]: ...
6060
def convert_timestamp(timestamp: float) -> float: ...
6161
def ass_escape(text: str) -> str: ...
62+
def convert_color(rgb: int, width: int = ..., height: int = ...): ...

packages/biliass/src/biliass/biliass.py

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
Comment,
1414
CommentPosition,
1515
ass_escape,
16+
convert_color,
1617
convert_timestamp,
1718
read_comments_from_protobuf,
1819
read_comments_from_xml,
@@ -467,45 +468,6 @@ def mark_comment_row(rows: list[Comment | None], comment: Comment, row: int):
467468
pass
468469

469470

470-
def ass_escape_py(s):
471-
def replace_leading_space(s):
472-
sstrip = s.strip(" ")
473-
slen = len(s)
474-
if slen == len(sstrip):
475-
return s
476-
else:
477-
llen = slen - len(s.lstrip(" "))
478-
rlen = slen - len(s.rstrip(" "))
479-
return "".join(("\u2007" * llen, sstrip, "\u2007" * rlen))
480-
481-
return "\\N".join(
482-
replace_leading_space(i) or " "
483-
for i in str(s).replace("\\", "\\\\").replace("{", "\\{").replace("}", "\\}").split("\n")
484-
)
485-
486-
487-
def convert_color(RGB, width=1280, height=576):
488-
if RGB == 0x000000:
489-
return "000000"
490-
elif RGB == 0xFFFFFF:
491-
return "FFFFFF"
492-
R = (RGB >> 16) & 0xFF
493-
G = (RGB >> 8) & 0xFF
494-
B = RGB & 0xFF
495-
if width < 1280 and height < 576:
496-
return f"{B:02X}{G:02X}{R:02X}"
497-
else: # VobSub always uses BT.601 colorspace, convert to BT.709
498-
499-
def clip_byte(x):
500-
return 255 if x > 255 else 0 if x < 0 else round(x)
501-
502-
return "{:02X}{:02X}{:02X}".format( # noqa: UP032
503-
clip_byte(R * 0.00956384088080656 + G * 0.03217254540203729 + B * 0.95826361371715607),
504-
clip_byte(R * -0.10493933142075390 + G * 1.17231478191855154 + B * -0.06737545049779757),
505-
clip_byte(R * 0.91348912373987645 + G * 0.07858536372532510 + B * 0.00792551253479842),
506-
)
507-
508-
509471
def convert_type2(row, height, bottom_reserved):
510472
return height - bottom_reserved - row
511473

0 commit comments

Comments
 (0)