定制 alto/scale 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

alto/scale

Composer 安装命令:

composer require alto/scale

包简介

A nano package for computing and manipulating modular scales (typography, rhythm, grids).

README 文档

README

A strict, mathematical engine for Modular Scales. It handles the arithmetic of progressions, designed for generative design, typography engines, and rhythmic computation.

  PHP Version   Packagist Version   CI   PHP Version   PHP Version   License

Installation

composer require alto/scale

Core Concepts

1. The Scale Facade

Use the Scale facade for fluent instantiation of any scale type.

use Alto\Scale\Scale;
use Alto\Scale\Ratio;

// Modular (Geometric): Base 16px, Ratio 1.25 (Major Third)
$type = Scale::majorThird(16.0);

// Linear (Arithmetic): Base 0, Increment 8
$grid = Scale::linear(0, 8);

// Fibonacci: Using Binet's formula
$fib = Scale::fibonacci();

// Multi-Strand: Interleaved scales (e.g., 16px and 12px strands)
$strands = Scale::strands([16, 12], Ratio::PerfectFifth);

2. Computing Values

All scales implement the ScaleInterface and are IteratorAggregate.

echo $type->get(0);  // 16.0
echo $type->get(1);  // 20.0
echo $type->get(-1); // 12.8

// ModularScale is callable
echo $type(2); // 25.0

Snapping (Normalization)

Find the nearest mathematical step for any arbitrary value.

// Returns 20.0 (The nearest step on a Major Third scale starting at 16)
$value = $type->snap(19.8);

Iteration & Ranges

// Scales are iterable (defaults to steps 0-10)
foreach ($type as $step => $value) {
    // ...
}

// Custom ranges
$values = $type->range(-2, 2);
// Returns: [-2 => 10.24, -1 => 12.8, 0 => 16.0, 1 => 20.0, 2 => 25.0]

3. The Guesser (Reverse Engineering)

Analyze messy datasets to find their underlying mathematical scale.

use Alto\Scale\Scale;
use Alto\Scale\ScaleGuesser;

$messyValues = [15.9, 20.1, 24.8, 31.5];

// Via the facade
$scale = Scale::guess($messyValues);

// Or directly with custom tolerance
$guesser = new ScaleGuesser(tolerance: 0.05);
$scale = $guesser->guess($messyValues);

// Align values to the guessed (or provided) scale
$aligned = $guesser->align($messyValues);

4. The Linter (Audit & Fix)

Audit datasets for scale compliance and automatically harmonize them.

use Alto\Scale\ScaleLinter;
use Alto\Scale\Scale;

$linter = new ScaleLinter(Scale::linear(0, 8));

// Get a detailed compliance report
$report = $linter->lint([8, 15, 24]);
// Each entry: ['original', 'suggested', 'step', 'deviation', 'isValid']

// Returns [8.0, 16.0, 24.0]
$fixed = $linter->fix([8, 15, 24]);

API Reference

Alto\Scale\Scale (Facade)

Method Returns
modular(float $base, float|Ratio $ratio) ModularScale
linear(float $base, float $increment) LinearScale
fibonacci(float $multiplier) FibonacciScale
strands(array $bases, float|Ratio $ratio) MultiStrandScale
guess(array $values) ModularScale
majorThird(float $base) ModularScale
perfectFifth(float $base) ModularScale
golden(float $base) ModularScale

Alto\Scale\ScaleInterface

All scale classes (ModularScale, LinearScale, FibonacciScale, MultiStrandScale) implement:

Method Description
get(int $step): float Get the value at a specific step
stepOf(float $value): int Find the closest step index for a value
snap(float $value): float Snap a value to the nearest step
range(int $min, int $max): array Generate values between steps

Alto\Scale\ModularScale

Geometric progression: value = base * (ratio ^ step)

Method Description
areHarmonic(float $a, float $b, float $epsilon): bool Check if two values are harmonically related
withBase(float $base): self Create a new scale with a different base
withRatio(float|Ratio $ratio): self Create a new scale with a different ratio
shift(int $steps): self Create a new scale shifted by N steps

Alto\Scale\LinearScale

Arithmetic progression: value = base + (step * increment)

Alto\Scale\FibonacciScale

Fibonacci sequence using Binet's formula, with optional multiplier.

Alto\Scale\MultiStrandScale

Interleaved modular scales sharing a common ratio, useful for multi-strand typographic scales.

Alto\Scale\ScaleGuesser

Method Description
guess(array $values): ModularScale Infer a modular scale from values
align(array $values, ?ScaleInterface $scale): array Snap all values to a scale

Alto\Scale\ScaleLinter

Method Description
lint(array $values): array Audit values against a scale
fix(array $values): array Harmonize values to the scale

Alto\Scale\Ratio (Enum)

Standard musical/typographic ratios:

Ratio Value
MinorSecond 1.067
MajorSecond 1.125
MinorThird 1.200
MajorThird 1.250
PerfectFourth 1.333
AugmentedFourth 1.414
PerfectFifth 1.500
MinorSixth 1.600
GoldenRatio 1.618
MajorSixth 1.667
MinorSeventh 1.778
MajorSeventh 1.875
Octave 2.000

License

This project is licensed under the MIT License.

alto/scale 适用场景与选型建议

alto/scale 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 01 月 16 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「typography」 「grid」 「scale」 「fibonacci」 「rhythm」 「modular-scale」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 alto/scale 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 alto/scale 我们能提供哪些服务?
定制开发 / 二次开发

基于 alto/scale 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 33
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-16