跳到主要内容

ColoredString 染色字符串类

ColoredString 是 Alia 的内置染色字符串类,用于表示带有颜色信息的字符串。它根据字符串模板 raw 和一个 Sheet(颜色与染色位置数组的映射关系)来染色字符串。

创建染色字符串​

使用构造函数​

const emptyCs = new ColoredString
const helloCs = new ColoredString ['Hello World']
const redColor = new Color ['#FF0000', 'Hex']
const colorSheet = new Sheet [['color', 'ranges'], [[redColor, [[0, 5]]]]]
const mappedCs = new ColoredString ['Hello World', colorSheet]

构造函数参数​

参数类型说明
strString原始字符串,默认为空字符串
colorsSheet颜色映射表,表头为 ['color', 'ranges']

构造调用采用数组形式:new ColoredString [str] 表示仅含原始字符串,new ColoredString [str, colors] 表示同时传入颜色映射表。new Color [value, type] 表示以指定类型创建颜色。

颜色映射表(Sheet)结构​

颜色映射表是一个 Sheet 对象,包含以下表头:

列名类型说明
colorColor颜色对象
rangesArray染色位置数组,格式为 [[from, to], ...]

示例:

const red = new Color ['#FF0000', 'Hex']
const blue = new Color ['#0000FF', 'Hex']
const colorSheet = new Sheet [['color', 'ranges'], [[red, [[0, 5], [10, 15]]], [blue, [[5, 10]]]]]

实例属性​

raw​

返回原始字符串。

const cs = new ColoredString ['Hello World']
print cs.raw

colors​

返回颜色映射表(Sheet 对象)。

const redColor = new Color ['#FF0000', 'Hex']
const colorSheet = new Sheet [['color', 'ranges'], [[redColor, [[0, 5]]]]]
const cs = new ColoredString ['Hello', colorSheet]
print cs.colors.headers

实例方法​

color <Value color> at <Value ranges>​

为指定区域染色。

参数:

  • color (Color): 颜色对象
  • ranges (Array): 染色范围,格式为 [from, to]

返回:返回当前 ColoredString 对象(支持链式调用)

const redColor = new Color ['#FF0000', 'Hex']
const cs = new ColoredString ['Hello World']
cs.color redColor at [0, 5]
const blueColor = new Color ['#0000FF', 'Hex']
cs.color blueColor at [6, 11]

add <String other> with <Value color>​

添加字符串并指定颜色。

参数:

  • other (String): 要添加的字符串
  • color (Color): 颜色对象

返回:返回当前 ColoredString 对象(支持链式调用)

const redColor = new Color ['#FF0000', 'Hex']
const cs = new ColoredString ['Hello']
cs.add ' World' with redColor
assert cs.raw == 'Hello World' error 'add result mismatch'
const blueColor = new Color ['#0000FF', 'Hex']
cs.add '!' with blueColor
assert cs.raw == 'Hello World!' error 'add chain mismatch'

toString​

返回字符串表示,其内容包含原始字符串与颜色映射描述。

const redColor = new Color ['#FF0000', 'Hex']
const cs = new ColoredString ['Hello']
cs.color redColor at [0, 5]
print call cs.toString

valueOf​

返回原始字符串。

const cs = new ColoredString ['Hello World']
print call cs.valueOf

示例代码​

const red = new Color ['#FF0000', 'Hex']
const green = new Color ['#00FF00', 'Hex']
const blue = new Color ['#0000FF', 'Hex']
const cs = new ColoredString ['Hello World']
cs.color red at [0, 5]
cs.color green at [6, 11]
cs.add '! ' with blue
cs.add 'Alia' with red
assert cs.raw == 'Hello World! Alia' error 'raw mismatch'
const colors = cs.colors
assert colors.data.length == 3 error 'colors length mismatch'
const redRanges = colors.lookup red returning 'ranges' in 'color'
print redRanges
const initSheet = new Sheet [['color', 'ranges'], [[red, [[0, 5]]], [blue, [[5, 10]]]]]
const cs2 = new ColoredString ['ABCDEFGHIJ', initSheet]
assert cs2.raw == 'ABCDEFGHIJ' error 'cs2 raw mismatch'

注意事项​

  1. 染色位置:[from, to] 表示从索引 from(包含)到 to(不包含)的字符范围
  2. 颜色映射表:内部使用 Sheet 对象存储,确保对象键必须是关键字的原则
  3. 对象引用:颜色查找基于对象引用(=== 比较),相同颜色值但不同对象会被视为不同颜色

下一步​