'use strict';
const Rac = require('../Rac');
const utils = require('../util/utils');
/**
* Fill [color]{@link Rac.Color} for drawing.
*
* Can be used as `fill.apply()` to apply the fill settings globally, or as
* the parameter of `drawable.draw(fill)` to apply the fill only for that
* `draw`.
*
* When `color` is `null` a *no-fill* setting is applied.
*
* @alias Rac.Fill
*/
class Fill {
/**
* Creates a new `Fill` instance.
*
* @param {Rac} rac - Instance to use for drawing and creating other objects
* @param {?Rac.Color} color - A `Color` for the fill setting, or `null`
* to apply a *no-fill* setting
*/
constructor(rac, color = null) {
utils.assertExists(rac);
color !== null && utils.assertType(Rac.Color, color);
/**
* Instance of `Rac` used for drawing and passed along to any created
* object.
*
* @type {Rac}
*/
this.rac = rac;
this.color = color;
}
/**
* Returns a `Fill` derived from `something`.
*
* + When `something` is an instance of `Fill`, returns that same object.
* + When `something` is an instance of `Color`, returns a new `Fill`
* using `something` as `color`.
* + When `something` is an instance of `Stroke`, returns a new `Fill`
* using `stroke.color`.
* + Otherwise an error is thrown.
*
* @param {Rac} rac - Instance to pass along to newly created objects
* @param {Rac.Fill|Rac.Color|Rac.Stroke} something - An object to
* derive a `Fill` from
* @returns {Rac.Fill}
*/
static from(rac, something) {
if (something instanceof Fill) {
return something;
}
if (something instanceof Rac.Color) {
return new Fill(rac, something);
}
if (something instanceof Rac.Stroke) {
return new Fill(rac, something.color);
}
throw Rac.Exception.invalidObjectType(
`Cannot derive Rac.Fill - something-type:${utils.typeName(something)}`);
}
styleWithStroke(someStroke) {
let stroke = Rac.Stroke.from(this.rac, someStroke);
return new Rac.Style(this.rac, stroke, this);
}
} // class Fill
module.exports = Fill;