BB.Color
A module for creating color objects, color schemes and doing color maths.
Index
Properties
Constructor
BB.Color
(
-
[r]
-
[g]
-
[b]
-
[a]
)
-
[r]
-
[g]
-
[b]
-
[a]
Parameters:
-
[r]
Number optionaloptional parameter for setting the red value (0-255)
-
[g]
Number optionaloptional parameter for setting the green value (0-255)
-
[b]
Number optionaloptional parameter for setting the blue value (0-255)
-
[a]
Number optionaloptional parameter for setting the alpha value (0-255)
Example:
var color = new BB.Color(255,0,0);
Properties
a (alpha)
Number
default:255
the alpha value between 0 - 255
b (blue)
Number
default:119
the blue value between 0 - 255
g (green)
Number
default:4
the green value between 0 - 255
h (hue)
Number
default:0
the hue value between 0 - 359
hex
String
default:"#e40477"
the base color's hex string
r (red)
Number
default:228
the red value between 0 - 255
rgb
String
default:"rgb(228,4,119)"
the base color's rgb string
rgba
String
default:"rgba(228,4,119,1)"
the base color's rgba string
s (saturation)
Number
default:0
the saturation value between 0 - 100
schemes
Object
object with properties ( named after different color schemes ) for
holding arrays of new BB.Color objects generated with the
createScheme()
method.
v (value)
Number
default:0
the brightness/lightness value between 0 - 100
Methods
clone
()
BB.Color
creates a new color object that is a copy of itself
Returns: BB.Color
a new color object copied from this one
Example:
var x = new color(0,255,0);
var y = x.clone();
y.rgb; // returns 'rgb(0,255,0)';
copy
(
-
color
)
BB.Color
chainable
-
color
createScheme
(
-
scheme
-
optional
)
-
scheme
-
optional
generates a color scheme ( array of additional color values ) from the base color.
the colors are stored in an array in the .schemes
property (
object ) and can be accessed by querying the key ( name ) of the color
scheme you generated like so: .schemes.triadic
, which
will return an array of BB.Color objects
Parameters:
-
scheme
Stringname of the color scheme you want to generate. can be either "monochromatic", "analogous", "complementary", "splitcomplementary", "triadic", "tetradic" or "random"
-
optional
Objectconfig object with properties for angle (Number) for hue shift ( for schemes other than "complimentary" or "triadic" which have fixed angles ), tint (Array of Floats) and shade (Array of Floats), which are used to create aditional monochromatic colors ( tint for light variations of the base color and shade for dark ) in relation to the base colors of each scheme
the "random" scheme takes an entirely different config object with values for hue, saturation and value. when no config is sent it generates entirely random colors. when a
{ hue: 200 }
is passed than you'd get random shades of blue, etc.if you need a color scheme/theory refersher: check this out
Example:
color.createScheme("complementary"); // creates single complementary color
// creates two analogous colors
// as well as 2 shades and 2 tints for each of the two analogous colors
// so color.schemes.analogous.length will be 10
color.createScheme("analogous",{
angle: 30,
tint:[ 0.4, 0.8 ],
shade:[ 0.3, 0.6 ]
});
color.createScheme("random",{
hue: 200,
});
color.schemes.analogous[0] // returns first analogous color
color.schemes.analogous[1] // returns second analogous color
color.schemes.random[0] // returns first random blue shade
hsv2rgb
(
-
[hsv]
-
[s]
-
[v]
)
Object
-
[hsv]
-
[s]
-
[v]
converts hsv values into rgb values, you can pass it an instance of BB.Color as a single parameter or pass it three individual parameters ( for h, s and v ) and it returns an object with r,g,b properties.
if you don't pass it any parameters it takes its own internal values as arguments and updates it's own internal rgb automatically ( that functionality is used internally, for ex. by the getters && setters )
Parameters:
-
[hsv]
Number optionaleither an instance of BB.Color or a h value between 0 - 359
-
[s]
Number optionala saturation value between 0 - 100
-
[v]
Number optionala brightness/lightness value value between 0 - 100
Returns: Object
an object with r, g, b properties
isEqual
(
-
color
-
excludeAlpha
)
Boolean
-
color
-
excludeAlpha
checks if another color object is equal to itself
Parameters:
-
color
BB.Coloranother color object to compare to
-
excludeAlpha
BooleanWhether or not to exlude Alpha property. True by default.
Returns: Boolean
true if it's equal, false if it's not
rgb2hsv
(
-
[rgb]
-
[g]
-
[b]
)
Object
-
[rgb]
-
[g]
-
[b]
converts rgb values into hsv values, you can pass it an instance of BB.Color as a single parameter or pass it three individual parameters ( for r, g and b ) and it returns an object with h,s,v properties.
if you don't pass it any parameters it takes its own internal values as arguments and updates it's own internal hsv automatically ( that functionality is used internally, for ex. by the getters && setters )
Parameters:
-
[rgb]
Number optionaleither an instance of BB.Color or a red value between 0 - 255
-
[g]
Number optionala green value between 0 - 255
-
[b]
Number optionala blue value value between 0 - 255
Returns: Object
an object with h, s, v properties
setHSVA
(
-
h
-
s
-
v
-
a
)
BB.Color
chainable
-
h
-
s
-
v
-
a
sets the h(hue) s(saturation) v(value) of the color
Parameters:
-
h
Numbersets the hue value from 0 - 359
-
s
Numbersets the saturation value from 0 - 100
-
v
Numbersets the light/bright value from 0 - 100
-
a
Numbersets the alpha value from 0 - 255
Returns: BB.Color
this color
setRGBA
(
-
r
-
g
-
b
-
a
)
BB.Color
chainable
-
r
-
g
-
b
-
a
sets the rgba value of the color
Parameters:
-
r
Numbersets the red value from 0 - 255
-
g
Numbersets the green value from 0 - 255
-
b
Numbersets the blue value from 0 - 255
-
a
Numbersets the alpha value from 0 - 255
Returns: BB.Color
this color
shade
(
-
percentage
)
BB.Color
chainable
-
percentage
changes the color by darkening it by a certain percentage
Parameters:
-
percentage
Numberfloat between 0 and 1
Returns: BB.Color
this color
shift
(
-
degrees
-
[hue]
)
BB.Color
chainable
-
degrees
-
[hue]
changes the color by shifting current hue value by a number of degrees, also chainable ( see example )
can also take an additional hue parameter when used as a utility ( see
example ), used this way internally by .createScheme
Parameters:
-
degrees
Numbernumber of degress to shift current hue by ( think rotating a color wheel )
-
[hue]
Number optionalThe hue parameter to use. Including this parameter changes the behavior of this function to act as a utility function.
Returns: BB.Color
this color
Example:
color.shift( 10 ); //
shifts by 10 degrees
var comp = color.clone().shift( 180 );
// new complementary color obj
// as a utility ( without
changing the color )
color.shift( 180, color.h ); // returns
the complementary hue ( in degrees )