tipstar0125 / ahc

ahc
0 stars 0 forks source link

Visualizer #9

Open tipstar0125 opened 6 months ago

tipstar0125 commented 6 months ago

https://yunix-kyopro.hatenablog.com/entry/2023/12/17/150534

tipstar0125 commented 6 months ago

image

tipstar0125 commented 5 months ago
tipstar0125 commented 5 months ago
let mut doc = Document::new()
    .set("ViewBox", (-5, -5, 600, 600))
    .set("width", 600)
    .set("height", 600)
    .set("stroke", "gray")
    .set("stroke-width", 1);
let size = 30.0;
let data = Data::new()
    .move_to((size, size))
    .line_by((0, size))
    .line_by((size, 0))
    .line_by((0, -size))
    .line_by((-size, 0));
let p = Path::new()
    .set("d", data)
    .set("stroke", "black")
    .set("stroke-width", 1)
    .set("fill", "green")
    .set("fill-opacity", 0.5);

let rec = Rectangle::new()
    .set("x", size * 3.0)
    .set("y", size)
    .set("width", size)
    .set("height", size)
    .set("stroke", "red")
    .set("stroke-opacity", 0.5)
    .set("stroke-width", 3)
    .set("fill", "green")
    .set("fill-opacity", 0.5);
// .set(
//     "transform",
//     format!("rotate(45,{},{})", size * 3.5, size * 1.5),
// );

let cir = Circle::new()
    .set("cx", size * 3.0)
    .set("cy", size * 3.0)
    .set("r", size);

let text = Text::new()
    // .add(TextContent::new(format!("{}", 2)))
    .add(TextContent::new("aaa"))
    .set("x", size * 1.5)
    .set("y", size * 1.5)
    // .set("fill", "black")
    // .set("font-size", "medium")
    .set("dominant-baseline", "central") // 上下中央揃え
    .set("text-anchor", "middle"); // 左右中央揃え
tipstar0125 commented 5 months ago
doc = doc.add(Style::new(format!(
    "text {{text-anchor: middle;dominant-baseline: central; font-size: {}}}",
    16 * 2 * input.n / input.m / max_h
)));
tipstar0125 commented 5 months ago

https://itsakura.com/html-color-codes

tipstar0125 commented 5 months ago
fn generate_dark_color(code: usize) -> String {
    // 入力値に基づいてHue(色相)を計算
    let hue = (code as f32 * 36.0) % 360.0;

    // Saturation(彩度)を低めに、Lightness(明度)を固定値で低く設定
    let saturation = 30.0;
    let lightness = 30.0;

    // HSL to RGB 変換
    let hue_normalized = hue / 360.0;
    let q = if lightness < 0.5 {
        lightness * (1.0 + saturation)
    } else {
        lightness + saturation - (lightness * saturation)
    };

    let p = 2.0 * lightness - q;

    let r = hue_to_rgb(p, q, hue_normalized + 1.0 / 3.0);
    let g = hue_to_rgb(p, q, hue_normalized);
    let b = hue_to_rgb(p, q, hue_normalized - 1.0 / 3.0);

    // RGB を 16 進数に変換して文字列を返す
    format!(
        "#{:02X}{:02X}{:02X}",
        (r * 255.0) as u8,
        (g * 255.0) as u8,
        (b * 255.0) as u8
    )
}

fn generate_color(code: usize) -> String {
    // 入力値に基づいてHue(色相)を計算
    let hue = (code as f32 * 36.0) % 360.0;

    // Saturation(彩度)とLightness(明度)を固定値で設定
    let saturation = 10.0;
    let lightness = 0.1;

    // HSL to RGB 変換
    let hue_normalized = hue / 360.0;
    let q = if lightness < 0.5 {
        lightness * (1.0 + saturation)
    } else {
        lightness + saturation - (lightness * saturation)
    };

    let p = 2.0 * lightness - q;

    let r = hue_to_rgb(p, q, hue_normalized + 1.0 / 3.0);
    let g = hue_to_rgb(p, q, hue_normalized);
    let b = hue_to_rgb(p, q, hue_normalized - 1.0 / 3.0);

    // RGB を 16 進数に変換して文字列を返す
    format!(
        "#{:02X}{:02X}{:02X}",
        (r * 255.0) as u8,
        (g * 255.0) as u8,
        (b * 255.0) as u8
    )
}

fn hue_to_rgb(p: f32, q: f32, t: f32) -> f32 {
    let t = if t < 0.0 {
        t + 1.0
    } else if t > 1.0 {
        t - 1.0
    } else {
        t
    };

    if t < 1.0 / 6.0 {
        p + (q - p) * 6.0 * t
    } else if t < 1.0 / 2.0 {
        q
    } else if t < 2.0 / 3.0 {
        p + (q - p) * (2.0 / 3.0 - t) * 6.0
    } else {
        p
    }
}
tipstar0125 commented 5 months ago
use svg::node::element::{Group, Rectangle, Title};
use svg::node::Text as TextContent;
use svg::Document;

pub fn group(title: String) -> Group {
    Group::new().add(Title::new().add(TextContent::new(title)))
}

fn main() {
    let mut doc = Document::new()
        .set("ViewBox", (-5, -5, 600, 600))
        .set("width", 600)
        .set("height", 600);

    let size = 30;
    let rec = Rectangle::new()
        .set("x", size)
        .set("y", size)
        .set("width", size)
        .set("height", size)
        .set("fill", "red");
    let mut grp = group(format!("x:{}\ny{}", size, size));
    grp = grp.add(rec);
    doc = doc.add(grp);

    let svg = doc.to_string();
    let vis = format!("<html><body>{}</body></html>", svg);
    std::fs::write("vis.html", vis).unwrap();
}
tipstar0125 commented 5 months ago

三角形