mirror of
https://github.com/atom/atom.git
synced 2026-01-24 06:18:03 -05:00
Add color schema type
This commit is contained in:
@@ -25,6 +25,7 @@
|
||||
"clear-cut": "0.4.0",
|
||||
"coffee-script": "1.8.0",
|
||||
"coffeestack": "0.8.0",
|
||||
"color": "^0.7.3",
|
||||
"delegato": "^1",
|
||||
"emissary": "^1.3.1",
|
||||
"event-kit": "^1.0.1",
|
||||
|
||||
@@ -1287,6 +1287,25 @@ describe "Config", ->
|
||||
atom.config.set 'foo.bar', ['2', '3', '4']
|
||||
expect(atom.config.get('foo.bar')).toEqual [2, 3, 4]
|
||||
|
||||
describe 'when the value has a "color" type', ->
|
||||
beforeEach ->
|
||||
schema =
|
||||
type: 'color'
|
||||
default: true
|
||||
atom.config.setSchema('foo.bar.aColor', schema)
|
||||
|
||||
it 'coerces various types to a color object', ->
|
||||
atom.config.set('foo.bar.aColor', 'red')
|
||||
expect(atom.config.get('foo.bar.aColor')).toEqual {red: 255, green: 0, blue: 0, alpha: 1}
|
||||
atom.config.set('foo.bar.aColor', '#020')
|
||||
expect(atom.config.get('foo.bar.aColor')).toEqual {red: 0, green: 34, blue: 0, alpha: 1}
|
||||
atom.config.set('foo.bar.aColor', '#abcdef')
|
||||
expect(atom.config.get('foo.bar.aColor')).toEqual {red: 171, green: 205, blue: 239, alpha: 1}
|
||||
atom.config.set('foo.bar.aColor', 'rgb(1,2,3)')
|
||||
expect(atom.config.get('foo.bar.aColor')).toEqual {red: 1, green: 2, blue: 3, alpha: 1}
|
||||
atom.config.set('foo.bar.aColor', 'rgba(4,5,6,.7)')
|
||||
expect(atom.config.get('foo.bar.aColor')).toEqual {red: 4, green: 5, blue: 6, alpha: .7}
|
||||
|
||||
describe 'when the `enum` key is used', ->
|
||||
beforeEach ->
|
||||
schema =
|
||||
|
||||
@@ -5,6 +5,7 @@ EmitterMixin = require('emissary').Emitter
|
||||
CSON = require 'season'
|
||||
path = require 'path'
|
||||
async = require 'async'
|
||||
Color = require 'color'
|
||||
pathWatcher = require 'pathwatcher'
|
||||
Grim = require 'grim'
|
||||
|
||||
@@ -1103,6 +1104,18 @@ Config.addSchemaEnforcers
|
||||
else
|
||||
value
|
||||
|
||||
'color':
|
||||
coerce: (keyPath, value, schema) ->
|
||||
try
|
||||
color = new Color(value)
|
||||
catch error
|
||||
throw new Error("Validation failed at #{keyPath}, #{JSON.stringify(value)} cannot be coerced into a color")
|
||||
|
||||
red: color.red()
|
||||
green: color.green()
|
||||
blue: color.blue()
|
||||
alpha: color.alpha()
|
||||
|
||||
'*':
|
||||
coerceMinimumAndMaximum: (keyPath, value, schema) ->
|
||||
return value unless typeof value is 'number'
|
||||
|
||||
Reference in New Issue
Block a user