OpenShot Library | libopenshot
1.1.1
Toggle main menu visibility
Loading...
Searching...
No Matches
Color.cpp
Go to the documentation of this file.
1
8
9
// Copyright (c) 2008-2019 OpenShot Studios, LLC
10
//
11
// SPDX-License-Identifier: LGPL-3.0-or-later
12
13
#include <utility>
14
#include <cmath>
15
16
#include "
Color.h
"
17
#include "
Exceptions.h
"
18
19
using namespace
openshot
;
20
21
// Constructor which takes R,G,B,A
22
Color::Color
(
unsigned
char
Red,
unsigned
char
Green,
unsigned
char
Blue,
unsigned
char
Alpha) :
23
red
(static_cast<double>(Red)),
24
green
(static_cast<double>(Green)),
25
blue
(static_cast<double>(Blue)),
26
alpha
(static_cast<double>(Alpha)) { }
27
28
// Constructor which takes 4 existing Keyframe curves
29
Color::Color
(
Keyframe
Red,
Keyframe
Green,
Keyframe
Blue,
Keyframe
Alpha) :
30
red
(Red),
green
(Green),
blue
(Blue),
alpha
(Alpha) { }
31
32
// Constructor which takes a QColor
33
Color::Color
(QColor qcolor) :
34
red
(qcolor.
red
()),
35
green
(qcolor.
green
()),
36
blue
(qcolor.
blue
()),
37
alpha
(qcolor.
alpha
()) { }
38
39
40
// Constructor which takes a HEX color code
41
Color::Color
(std::string color_hex)
42
:
Color
::
Color
(QString::fromStdString(color_hex)) {}
43
44
Color::Color
(
const
char
* color_hex)
45
:
Color
::
Color
(QString(color_hex)) {}
46
47
// Get the HEX value of a color at a specific frame
48
std::string
Color::GetColorHex
(int64_t frame_number) {
49
50
int
r =
red
.GetInt(frame_number);
51
int
g =
green
.GetInt(frame_number);
52
int
b =
blue
.GetInt(frame_number);
53
int
a =
alpha
.GetInt(frame_number);
54
55
return
QColor( r,g,b,a ).name().toStdString();
56
}
57
58
// Get RGBA values for a specific frame as an integer vector
59
std::vector<int>
Color::GetColorRGBA
(int64_t frame_number) {
60
std::vector<int> rgba;
61
rgba.push_back(
red
.GetInt(frame_number));
62
rgba.push_back(
green
.GetInt(frame_number));
63
rgba.push_back(
blue
.GetInt(frame_number));
64
rgba.push_back(
alpha
.GetInt(frame_number));
65
66
return
rgba;
67
}
68
69
// Get the distance between 2 RGB pairs (alpha is ignored)
70
long
Color::GetDistance
(
long
R1,
long
G1,
long
B1,
long
R2,
long
G2,
long
B2)
71
{
72
long
rmean = ( R1 + R2 ) / 2;
73
long
r = R1 - R2;
74
long
g = G1 - G2;
75
long
b = B1 - B2;
76
return
sqrt((((512+rmean)*r*r)>>8) + 4*g*g + (((767-rmean)*b*b)>>8));
77
}
78
79
// Generate JSON string of this object
80
std::string
Color::Json
()
const
{
81
82
// Return formatted string
83
return
JsonValue
().toStyledString();
84
}
85
86
// Generate Json::Value for this object
87
Json::Value
Color::JsonValue
()
const
{
88
89
// Create root json object
90
Json::Value root;
91
root[
"red"
] =
red
.JsonValue();
92
root[
"green"
] =
green
.JsonValue();
93
root[
"blue"
] =
blue
.JsonValue();
94
root[
"alpha"
] =
alpha
.JsonValue();
95
96
// return JsonValue
97
return
root;
98
}
99
100
// Load JSON string into this object
101
void
Color::SetJson
(
const
std::string value) {
102
103
// Parse JSON string into JSON objects
104
try
105
{
106
Json::Value root =
openshot::stringToJson
(value);
107
// Set all values that match
108
SetJsonValue
(std::move(root));
109
}
110
catch
(
const
std::exception& e)
111
{
112
// Error parsing JSON (or missing keys)
113
throw
InvalidJSON
(
"JSON is invalid (missing keys or invalid data types)"
);
114
}
115
}
116
117
// Load Json::Value into this object
118
void
Color::SetJsonValue
(Json::Value root) {
119
120
// Set data from Json (if key is found)
121
if
(!root[
"red"
].isNull())
122
red
.SetJsonValue(std::move(root[
"red"
]));
123
if
(!root[
"green"
].isNull())
124
green
.SetJsonValue(std::move(root[
"green"
]));
125
if
(!root[
"blue"
].isNull())
126
blue
.SetJsonValue(std::move(root[
"blue"
]));
127
if
(!root[
"alpha"
].isNull())
128
alpha
.SetJsonValue(std::move(root[
"alpha"
]));
129
}
Color.h
Header file for Color class.
Exceptions.h
Header file for all Exception classes.
openshot::Color::Json
std::string Json() const
Generate JSON string of this object.
Definition
Color.cpp:80
openshot::Color::GetColorHex
std::string GetColorHex(int64_t frame_number)
Get the HEX value of a color at a specific frame.
Definition
Color.cpp:48
openshot::Color::blue
openshot::Keyframe blue
Curve representing the red value (0 - 255).
Definition
Color.h:32
openshot::Color::red
openshot::Keyframe red
Curve representing the red value (0 - 255).
Definition
Color.h:30
openshot::Color::green
openshot::Keyframe green
Curve representing the green value (0 - 255).
Definition
Color.h:31
openshot::Color::Color
Color()
Default constructor.
Definition
Color.h:36
openshot::Color::SetJsonValue
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition
Color.cpp:118
openshot::Color::alpha
openshot::Keyframe alpha
Curve representing the alpha value (0 - 255).
Definition
Color.h:33
openshot::Color::GetDistance
static long GetDistance(long R1, long G1, long B1, long R2, long G2, long B2)
Get the distance between 2 RGB pairs. (0=identical colors, 10=very close colors, 760=very different c...
Definition
Color.cpp:70
openshot::Color::GetColorRGBA
std::vector< int > GetColorRGBA(int64_t frame_number)
Definition
Color.cpp:59
openshot::Color::SetJson
void SetJson(const std::string value)
Load JSON string into this object.
Definition
Color.cpp:101
openshot::Color::JsonValue
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition
Color.cpp:87
openshot::InvalidJSON
Exception for invalid JSON.
Definition
Exceptions.h:224
openshot::Keyframe
A Keyframe is a collection of Point instances, which is used to vary a number or property over time.
Definition
KeyFrame.h:53
openshot
This namespace is the default namespace for all code in the openshot library.
Definition
AnimatedCurve.h:24
openshot::stringToJson
const Json::Value stringToJson(const std::string value)
Definition
Json.cpp:16
Color.cpp
Generated by
1.17.0