toastnz/colourpalettes
Composer 安装命令:
composer require toastnz/colourpalettes
包简介
Silverstripe colour palettes module
README 文档
README
This module allows you to manage and use colour palettes in your SilverStripe project.
Installation
To install the module, use Composer:
composer require toastnz/colourpalettes
Adding and editing colours
Colours can be managed in the CMS site config under 'Customisation' -> 'Colour Palettes'. Here you can add, edit and delete colours.
Colour palettes configuration
You can define default colours as well as colour groups in a yml config file. Default colours need to be hex codes without the #, contract colours are more customisable, and can be set to other variables, therefore a # is required if you want to use a hex code.
--- Name: colours After: colourpalettes --- Toast\ColourPalettes\Models\Colour: colour_groups: - Products - Buttons default_colours: - primary: null - secondary: null - white: 'ffffff' - black: '000000' - off-white: null - off-black: null contrast_colours: - on-dark: '#ffffff' - on-light: '#000000'
Adding colour groups will add a listbox to each colour added in the site config, allowing you to assign a colour to a single group or multiple groups.
Colours with a value will be locked and cannot be deleted or edited in the CMS. Groups are optional, but can be used to separate colours into various colour palette fields.
Contrast colours are the colours used when generating the css variables, on-dark will be used if the colour is set to dark, and on-light will be used if the colour is set to light. Makes senses right?
Adding Colour Palette Field to a Class
To add a colour palette field to a class, you can use the following code as an example:
use Toast\ColourPalettes\Models\ColourPalette; use Toast\ColourPalettes\Fields\ColourPaletteField; class YourClass extends DataObject { private static $has_one = [ 'PrimaryColour' => Colour::class, 'SecondaryColour' => Colour::class, ]; public function updateCMSFields(FieldList $fields) { $fields->removeByName([ 'PrimaryColourID', 'SecondaryColourID', ]); // Add the colour palette field to the class $fields->addFieldToTab('Root.Main', ColourPaletteField::create('PrimaryColour', 'Primary Colour')); // Optionally add a groups as an array to the field as the 3rd parameter $fields->addFieldToTab('Root.Main', ColourPaletteField::create('SecondaryColour', 'Secondary Colour', ['Products'])); } }
Template usage
To use the colours in your templates, you can use the following code as an example:
<!-- Add styles to the root -->
<style>
:root {
{$PrimaryColour.RootVars}
{$SecondaryColour.RootVars}
}
</style>
<!-- Add styles in a style tag scoped to this element -->
<div class="my-element">
<p>lorem ipsum</p>
<style>
.my-element {
{$PrimaryColour.CSSVars}
{$SecondaryColour.CSSVars}
}
</style>
</div>
Using the colours in your CSS
The colours will automatically generate their own CSS variables based on the name you give them on the class. For example, if you have a colour field called 'PrimaryColour', the following CSS variables will be generated:
/* $PrimaryColour.RootVars */ :root { --primary-colour: #000000; --primary-colour-contrast: #ffffff; } /* PrimaryColour.CSSVars */ .my-element { --_primary-colour: #000000; --_primary-colour-contrast: #ffffff; }
Helper functions
Sometimes you just want to know if the colour is light or dark:
<!-- Check if the colour is light or dark --> <% if $PrimaryColour.IsLight %> <p>Primary colour is light</p> <% end_if %> <% if $PrimaryColour.IsDark %> <p>Primary colour is dark</p> <% end_if %>
toastnz/colourpalettes 适用场景与选型建议
toastnz/colourpalettes 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.85k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 01 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「modular」 「silverstripe」 「colours」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 toastnz/colourpalettes 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 toastnz/colourpalettes 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 toastnz/colourpalettes 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Let clients choose from a predefined set of colours and utilise associated colour codes and class names in your templates.
A slick color picker fieldtype plugin for the Craft CMS 3 control panel.
Identify your matrix blocks by giving each type a different color.
Silverstripe content blocks module
Silverstripe theme colours module
统计信息
- 总下载量: 2.85k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 26
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2025-01-05