1use crate::internal_prelude::*;
2use crate::{qreal, QString};
3
4cpp! {{
5 #include <QtGui/QColor>
6 #include <QtCore/QString>
7}}
8
9#[derive(Copy, Clone, Eq, PartialEq, Hash)]
10pub struct QRgb(u32);
11
12impl QRgb {
13 pub fn alpha(&self) -> u8 {
14 ((self.0 >> 24) & 0x000000ff) as u8
15 }
16 pub fn red(&self) -> u8 {
17 ((self.0 >> 16) & 0x000000ff) as u8
18 }
19 pub fn green(&self) -> u8 {
20 ((self.0 >> 8) & 0x000000ff) as u8
21 }
22 pub fn blue(&self) -> u8 {
23 (self.0 & 0x000000ff) as u8
24 }
25}
26
27impl From<u32> for QRgb {
28 fn from(val: u32) -> QRgb {
29 QRgb(val)
30 }
31}
32
33impl Into<u32> for QRgb {
34 fn into(self) -> u32 {
35 self.0
36 }
37}
38
39#[derive(Copy, Clone, Eq, PartialEq, Hash)]
40pub struct QRgba64(u64);
41impl QRgba64 {
42 pub fn alpha(&self) -> u16 {
43 ((self.0 >> 48) & 0x0000ffff) as u16
44 }
45 pub fn red(&self) -> u16 {
46 ((self.0 >> 32) & 0x0000ffff) as u16
47 }
48 pub fn green(&self) -> u16 {
49 ((self.0 >> 16) & 0x0000ffff) as u16
50 }
51 pub fn blue(&self) -> u16 {
52 (self.0 & 0x0000ffff) as u16
53 }
54}
55
56impl From<u64> for QRgba64 {
57 fn from(val: u64) -> QRgba64 {
58 QRgba64(val)
59 }
60}
61
62impl Into<u64> for QRgba64 {
63 fn into(self) -> u64 {
64 self.0
65 }
66}
67
68/// Bindings for [`QColor::NameFormat`][class] enum class.
69///
70/// [class]: https://doc.qt.io/qt-5/qcolor.html#NameFormat-enum
71#[repr(C)]
72#[derive(Clone, Copy, PartialEq, Debug)]
73#[allow(non_camel_case_types)]
74pub enum QColorNameFormat {
75 /// #RRGGBB A "#" character followed by three two-digit hexadecimal numbers (i.e. #RRGGBB).
76 HexRgb = 0,
77 ///#AARRGGBB A "#" character followed by four two-digit hexadecimal numbers (i.e. #AARRGGBB).
78 HexArgb = 1,
79}
80
81/// Bindings for [`QColor::Spec`][class] enum class.
82///
83/// [class]: https://doc.qt.io/qt-5/qcolor.html#Spec-enum
84#[repr(C)]
85#[derive(Clone, Copy, PartialEq, Debug)]
86#[allow(non_camel_case_types)]
87pub enum QColorSpec {
88 Invalid = 0,
89 Rgb = 1,
90 Hsv = 2,
91 Cmyk = 3,
92 Hsl = 4,
93 ExtendedRgb = 5,
94}
95
96/// Bindings for [`Qt::GlobalColor`][class] enum.
97///
98/// [class]: https://doc.qt.io/qt-5/qt.html#GlobalColor-enum
99#[repr(C)]
100#[derive(Clone, Copy, PartialEq, Debug)]
101pub enum QGlobalColor {
102 Color0 = 0,
103 Color1 = 1,
104 Black = 2,
105 White = 3,
106 DarkGray = 4,
107 Gray = 5,
108 LightGray = 6,
109 Red = 7,
110 Green = 8,
111 Blue = 9,
112 Cyan = 10,
113 Magenta = 11,
114 Yellow = 12,
115 DarkRed = 13,
116 DarkGreen = 14,
117 DarkBlue = 15,
118 DarkCyan = 16,
119 DarkMagenta = 17,
120 Darkyellow = 18,
121 Transparent = 19,
122}
123
124cpp_class!(
125 /// Wrapper around [`QColor`][class] class.
126 ///
127 /// [class]: https://doc.qt.io/qt-5/qcolor.html
128 #[derive(Default, Clone, Copy, PartialEq)]
129 pub unsafe struct QColor as "QColor"
130);
131
132impl QColor {
133 /// Wrapper around [`QColor(QLatin1String)`][ctor] constructor.
134 ///
135 /// [ctor]: https://doc.qt.io/qt-5/qcolor.html#QColor-8
136 pub fn from_name(name: &str) -> Self {
137 let len = name.len();
138 let ptr = name.as_ptr();
139 cpp!(unsafe [len as "size_t", ptr as "char*"] -> QColor as "QColor" {
140 return QColor(QLatin1String(ptr, len));
141 })
142 }
143
144 /// Wrapper around [`QColor(Qt::GlobalColor)`][ctor] constructor.
145 ///
146 /// [ctor]: https://doc.qt.io/qt-5/qcolor.html#QColor-1
147 pub fn from_global_color(color: QGlobalColor) -> Self {
148 cpp!(unsafe [color as "Qt::GlobalColor"] -> QColor as "QColor" {
149 return QColor(color);
150 })
151 }
152
153 /*
154 * ==============
155 * STATIC MEMBERS
156 * ==============
157 */
158
159 // fn colorNames() -> QStringList;
160
161 pub fn from_cmyk(c: i32, m: i32, y: i32, k: i32) -> QColor {
162 Self::from_cmyka(c, m, y, k, 255)
163 }
164
165 pub fn from_cmyka(c: i32, m: i32, y: i32, k: i32, a: i32) -> QColor {
166 cpp!(unsafe [c as "int", m as "int", y as "int", k as "int", a as "int"] -> QColor as "QColor" {
167 return QColor::fromCmyk(c, m, y, k, a);
168 })
169 }
170
171 pub fn from_cmyk_f(c: qreal, m: qreal, y: qreal, k: qreal) -> QColor {
172 Self::from_cmyka_f(c, m, y, k, 1.0)
173 }
174
175 pub fn from_cmyka_f(c: qreal, m: qreal, y: qreal, k: qreal, a: qreal) -> QColor {
176 cpp!(unsafe [c as "qreal", m as "qreal", y as "qreal", k as "qreal", a as "qreal"] -> QColor as "QColor" {
177 return QColor::fromCmykF(c, m, y, k, a);
178 })
179 }
180
181 pub fn from_hsl(h: i32, s: i32, l: i32) -> QColor {
182 Self::from_hsla(h, s, l, 255)
183 }
184
185 pub fn from_hsla(h: i32, s: i32, l: i32, a: i32) -> QColor {
186 cpp!(unsafe [h as "int", s as "int", l as "int", a as "int"] -> QColor as "QColor" {
187 return QColor::fromHsl(h, s, l, a);
188 })
189 }
190
191 pub fn from_hsl_f(h: qreal, s: qreal, l: qreal) -> QColor {
192 Self::from_hsla_f(h, s, l, 1.0)
193 }
194
195 pub fn from_hsla_f(h: qreal, s: qreal, l: qreal, a: qreal) -> QColor {
196 cpp!(unsafe [h as "qreal", s as "qreal", l as "qreal", a as "qreal"] -> QColor as "QColor" {
197 return QColor::fromHslF(h, s, l, a);
198 })
199 }
200
201 pub fn from_hsv(h: i32, s: i32, v: i32) -> QColor {
202 Self::from_hsva(h, s, v, 255)
203 }
204
205 pub fn from_hsva(h: i32, s: i32, v: i32, a: i32) -> QColor {
206 cpp!(unsafe [h as "int", s as "int", v as "int", a as "int"] -> QColor as "QColor" {
207 return QColor::fromHsv(h, s, v, a);
208 })
209 }
210
211 pub fn from_hsv_f(h: qreal, s: qreal, v: qreal) -> QColor {
212 Self::from_hsva_f(h, s, v, 1.0)
213 }
214
215 pub fn from_hsva_f(h: qreal, s: qreal, v: qreal, a: qreal) -> QColor {
216 cpp!(unsafe [h as "qreal", s as "qreal", v as "qreal", a as "qreal"] -> QColor as "QColor" {
217 return QColor::fromHsvF(h, s, v, a);
218 })
219 }
220
221 pub fn from_rgb(r: i32, g: i32, b: i32) -> QColor {
222 Self::from_rgba(r, g, b, 255)
223 }
224
225 pub fn from_rgba(r: i32, g: i32, b: i32, a: i32) -> QColor {
226 cpp!(unsafe [r as "int", g as "int", b as "int", a as "int"] -> QColor as "QColor" {
227 return QColor::fromRgb(r, g, b, a);
228 })
229 }
230
231 /// Wrapper around [`fromRgbF(qreal r, qreal g, qreal b, qreal a = 1.0)`][ctor] constructor.
232 ///
233 /// # Wrapper-specific
234 ///
235 /// Alpha is left at default `1.0`. To set it to something other that 1.0, use [`from_rgba_f`][].
236 ///
237 /// [ctor]: https://doc.qt.io/qt-5/qcolor.html#fromRgbF
238 /// [`from_rgba_f`]: #method.from_rgba_f
239 pub fn from_rgb_f(r: qreal, g: qreal, b: qreal) -> QColor {
240 cpp!(unsafe [r as "qreal", g as "qreal", b as "qreal"] -> QColor as "QColor" {
241 return QColor::fromRgbF(r, g, b);
242 })
243 }
244
245 /// Wrapper around [`fromRgbF(qreal r, qreal g, qreal b, qreal a = 1.0)`][ctor] constructor.
246 ///
247 /// # Wrapper-specific
248 ///
249 /// Same as [`from_rgb_f`][], but accept an alpha value
250 ///
251 /// [ctor]: https://doc.qt.io/qt-5/qcolor.html#fromRgbF
252 /// [`from_rgb_f`]: #method.from_rgb_f
253 pub fn from_rgba_f(r: qreal, g: qreal, b: qreal, a: qreal) -> Self {
254 cpp!(unsafe [r as "qreal", g as "qreal", b as "qreal", a as "qreal"] -> QColor as "QColor" {
255 return QColor::fromRgbF(r, g, b, a);
256 })
257 }
258
259 pub fn from_rgb64(r: u16, g: u16, b: u16) -> QColor {
260 Self::from_rgba64(r, g, b, u16::MAX)
261 }
262
263 pub fn from_rgba64(r: u16, g: u16, b: u16, a: u16) -> QColor {
264 cpp!(unsafe [r as "unsigned short", g as "unsigned short", b as "unsigned short", a as "unsigned short"] -> QColor as "QColor" {
265 return QColor::fromRgba64(r, g, b, a);
266 })
267 }
268
269 pub fn from_qrgba64(rgba64: QRgba64) -> QColor {
270 let rgba64: u64 = rgba64.0;
271 cpp!(unsafe [rgba64 as "QRgba64"] -> QColor as "QColor" {
272 return QColor::fromRgba64(rgba64);
273 })
274 }
275
276 pub fn from_qrgb(rgb: QRgb) -> QColor {
277 let rgb: u32 = rgb.0;
278 cpp!(unsafe [rgb as "QRgb"] -> QColor as "QColor" {
279 return QColor::fromRgb(rgb);
280 })
281 }
282
283 pub fn is_valid_color(name: &str) -> bool {
284 let len = name.len();
285 let ptr = name.as_ptr();
286
287 cpp!(unsafe [len as "size_t", ptr as "char*"] -> bool as "bool" {
288 return QColor::isValidColor(QLatin1String(ptr, len));
289 })
290 }
291
292 /*
293 * ==============
294 * Public MEMBERS
295 * ==============
296 */
297 pub fn alpha(&self) -> i32 {
298 cpp!(unsafe [self as "const QColor*"] -> i32 as "int" {
299 return self->alpha();
300 })
301 }
302
303 pub fn alpha_f(&self) -> qreal {
304 cpp!(unsafe [self as "const QColor*"] -> qreal as "qreal" {
305 return self->alphaF();
306 })
307 }
308
309 pub fn black(&self) -> i32 {
310 cpp!(unsafe [self as "const QColor*"] -> i32 as "int" {
311 return self->black();
312 })
313 }
314
315 pub fn black_f(&self) -> qreal {
316 cpp!(unsafe [self as "const QColor*"] -> qreal as "qreal" {
317 return self->blackF();
318 })
319 }
320
321 pub fn blue(&self) -> i32 {
322 cpp!(unsafe [self as "const QColor*"] -> i32 as "int" {
323 return self->blue();
324 })
325 }
326
327 pub fn blue_f(&self) -> qreal {
328 cpp!(unsafe [self as "const QColor*"] -> qreal as "qreal" {
329 return self->blueF();
330 })
331 }
332
333 pub fn convert_to(&self, color_spec: QColorSpec) -> QColor {
334 cpp!(unsafe [self as "const QColor*", color_spec as "QColor::Spec"] -> QColor as "QColor" {
335 return self->convertTo(color_spec);
336 })
337 }
338
339 pub fn cyan(&self) -> i32 {
340 cpp!(unsafe [self as "const QColor*"] -> i32 as "int" {
341 return self->cyan();
342 })
343 }
344
345 pub fn cyan_f(&self) -> qreal {
346 cpp!(unsafe [self as "const QColor*"] -> qreal as "qreal" {
347 return self->cyanF();
348 })
349 }
350
351 pub fn darker(&self, factor: Option<i32>) -> QColor {
352 let factor = match factor {
353 Some(factor) => factor,
354 None => 200,
355 };
356
357 cpp!(unsafe [self as "const QColor*", factor as "int"] -> QColor as "QColor" {
358 return self->darker(factor);
359 })
360 }
361
362 /// This function should be const but at least in my local include (5.12) it is not marked as const and causes compilation to fail
363 /// > void getCmyk(int *c, int *m, int *y, int *k, int *a = nullptr);
364 pub fn get_cmyka(&mut self) -> (i32, i32, i32, i32, i32) {
365 let res = (0, 0, 0, 0, 0);
366 let (ref c, ref m, ref y, ref k, ref a) = res;
367 cpp!(unsafe [self as "QColor*", c as "int*", m as "int*", y as "int*", k as "int*", a as "int*"] {
368 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
369 int c_, m_, y_, k_, a_;
370 self->getCmyk(&c_, &m_, &y_, &k_, &a_);
371 *c = c_; *m = m_; *y = y_; *k = k_; *a = a_;
372 #else
373 self->getCmyk(c, m, y, k, a);
374 #endif
375 });
376 res
377 }
378
379 /// This function should be const but at least in my local include (5.12) it is not marked as const and causes compilation to fail
380 /// > void getCmykF(qreal *c, qreal *m, qreal *y, qreal *k, qreal *a = nullptr);
381 pub fn get_cmyka_f(&mut self) -> (qreal, qreal, qreal, qreal, qreal) {
382 let res = (0., 0., 0., 0., 0.);
383 let (ref c, ref m, ref y, ref k, ref a) = res;
384 cpp!(unsafe [self as "QColor*", c as "qreal*", m as "qreal*", y as "qreal*", k as "qreal*", a as "qreal*"] {
385 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
386 float c_, m_, y_, k_, a_;
387 self->getCmykF(&c_, &m_, &y_, &k_, &a_);
388 *c = c_; *m = m_; *y = y_; *k = k_; *a = a_;
389 #else
390 self->getCmykF(c, m, y, k, a);
391 #endif
392 });
393 res
394 }
395
396 pub fn get_hsla(&self) -> (i32, i32, i32, i32) {
397 let res = (0, 0, 0, 0);
398 let (ref h, ref s, ref l, ref a) = res;
399 cpp!(unsafe [self as "const QColor*", h as "int*", s as "int*", l as "int*", a as "int*"] {
400 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
401 int h_, s_, l_, a_;
402 self->getHsl(&h_, &s_, &l_, &a_);
403 *h = h_; *s = s_; *l = l_; *a = a_;
404 #else
405 self->getHsl(h, s, l, a);
406 #endif
407 });
408 res
409 }
410
411 pub fn get_hsla_f(&self) -> (qreal, qreal, qreal, qreal) {
412 let res = (0., 0., 0., 0.);
413 let (ref h, ref s, ref l, ref a) = res;
414 cpp!(unsafe [self as "const QColor*", h as "qreal*", s as "qreal*", l as "qreal*", a as "qreal*"] {
415 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
416 float h_, s_, l_, a_;
417 self->getHslF(&h_, &s_, &l_, &a_);
418 *h = h_; *s = s_; *l = l_; *a = a_;
419 #else
420 return self->getHslF(h, s, l, a);
421 #endif
422 });
423 res
424 }
425
426 pub fn get_hsva(&self) -> (i32, i32, i32, i32) {
427 let res = (0, 0, 0, 0);
428 let (ref h, ref s, ref v, ref a) = res;
429 cpp!(unsafe [self as "const QColor*", h as "int*", s as "int*", v as "int*", a as "int*"] {
430 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
431 int h_, s_, v_, a_;
432 self->getHsv(&h_, &s_, &v_, &a_);
433 *h = h_; *s = s_; *v = v_; *a = a_;
434 #else
435 self->getHsv(h, s, v, a);
436 #endif
437 });
438 res
439 }
440
441 pub fn get_hsva_f(&self) -> (qreal, qreal, qreal, qreal) {
442 let res = (0., 0., 0., 0.);
443 let (ref h, ref s, ref v, ref a) = res;
444 cpp!(unsafe [self as "const QColor*", h as "qreal*", s as "qreal*", v as "qreal*", a as "qreal*"] {
445 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
446 float h_, s_, v_, a_;
447 self->getHsvF(&h_, &s_, &v_, &a_);
448 *h = h_; *s = s_; *v = v_; *a = a_;
449 #else
450 return self->getHsvF(h, s, v, a);
451 #endif
452 });
453 res
454 }
455
456 /// Wrapper around [`getRgbF(qreal *r, qreal *g, qreal *b, qreal *a = nullptr)`][method] method.
457 ///
458 /// # Wrapper-specific
459 ///
460 /// Returns red, green, blue and alpha components as a tuple, instead of mutable references.
461 ///
462 /// [method]: https://doc.qt.io/qt-5/qcolor.html#getRgbF
463 pub fn get_rgba(&self) -> (i32, i32, i32, i32) {
464 let res = (0, 0, 0, 0);
465 let (ref r, ref g, ref b, ref a) = res;
466 cpp!(unsafe [self as "const QColor*", r as "int*", g as "int*", b as "int*", a as "int*"] {
467 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
468 int r_, g_, b_, a_;
469 self->getRgb(&r_, &g_, &b_, &a_);
470 *r = r_; *g = g_; *b = b_; *a = a_;
471 #else
472 return self->getRgb(r, g, b, a);
473 #endif
474 });
475 res
476 }
477
478 pub fn get_rgba_f(&self) -> (qreal, qreal, qreal, qreal) {
479 let res = (0., 0., 0., 0.);
480 let (ref r, ref g, ref b, ref a) = res;
481 cpp!(unsafe [self as "const QColor*", r as "qreal*", g as "qreal*", b as "qreal*", a as "qreal*"] {
482 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
483 float r_, g_, b_, a_;
484 self->getRgbF(&r_, &g_, &b_, &a_);
485 *r = r_; *g = g_; *b = b_; *a = a_;
486 #else
487 return self->getRgbF(r, g, b, a);
488 #endif
489 });
490 res
491 }
492
493 pub fn green(&self) -> i32 {
494 cpp!(unsafe [self as "const QColor*"] -> i32 as "int" {
495 return self->green();
496 })
497 }
498
499 pub fn green_f(&self) -> qreal {
500 cpp!(unsafe [self as "const QColor*"] -> qreal as "qreal" {
501 return self->greenF();
502 })
503 }
504
505 pub fn hsl_hue(&self) -> i32 {
506 cpp!(unsafe [self as "const QColor*"] -> i32 as "int" {
507 return self->hslHue();
508 })
509 }
510 pub fn hsl_hue_f(&self) -> qreal {
511 cpp!(unsafe [self as "const QColor*"] -> qreal as "qreal" {
512 return self->hslHueF();
513 })
514 }
515 pub fn hsl_saturation(&self) -> i32 {
516 cpp!(unsafe [self as "const QColor*"] -> i32 as "int" {
517 return self->hslSaturation();
518 })
519 }
520 pub fn hsl_saturation_f(&self) -> qreal {
521 cpp!(unsafe [self as "const QColor*"] -> qreal as "qreal" {
522 return self->hslSaturationF();
523 })
524 }
525 pub fn hsv_hue(&self) -> i32 {
526 cpp!(unsafe [self as "const QColor*"] -> i32 as "int" {
527 return self->hsvHue();
528 })
529 }
530 pub fn hsv_hue_f(&self) -> qreal {
531 cpp!(unsafe [self as "const QColor*"] -> qreal as "qreal" {
532 return self->hsvHueF();
533 })
534 }
535
536 pub fn hsv_saturation(&self) -> i32 {
537 cpp!(unsafe [self as "const QColor*"] -> i32 as "int" {
538 return self->hsvSaturation();
539 })
540 }
541
542 pub fn hsv_saturation_f(&self) -> qreal {
543 cpp!(unsafe [self as "const QColor*"] -> qreal as "qreal" {
544 return self->hsvSaturationF();
545 })
546 }
547
548 pub fn is_valid(&self) -> bool {
549 cpp!(unsafe [self as "const QColor*"] -> bool as "bool" {
550 return self->isValid();
551 })
552 }
553
554 pub fn lighter(&self, factor: Option<i32>) -> QColor {
555 let factor = match factor {
556 Some(factor) => factor,
557 None => 150,
558 };
559
560 cpp!(unsafe [self as "const QColor*", factor as "int"] -> QColor as "QColor" {
561 return self->lighter(factor);
562 })
563 }
564
565 pub fn lightness(&self) -> i32 {
566 cpp!(unsafe [self as "const QColor*"] -> i32 as "int" {
567 return self->lightness();
568 })
569 }
570
571 pub fn lightness_f(&self) -> qreal {
572 cpp!(unsafe [self as "const QColor*"] -> qreal as "qreal" {
573 return self->lightnessF();
574 })
575 }
576
577 pub fn magenta(&self) -> i32 {
578 cpp!(unsafe [self as "const QColor*"] -> i32 as "int" {
579 return self->magenta();
580 })
581 }
582
583 pub fn magenta_f(&self) -> qreal {
584 cpp!(unsafe [self as "const QColor*"] -> qreal as "qreal" {
585 return self->magentaF();
586 })
587 }
588
589 pub fn name(&self) -> QString {
590 cpp!(unsafe [self as "const QColor*"] -> QString as "QString" {
591 return self->name();
592 })
593 }
594
595 pub fn name_with_format(&self, format: QColorNameFormat) -> QString {
596 cpp!(unsafe [self as "const QColor*", format as "QColor::NameFormat"] -> QString as "QString" {
597 return self->name(format);
598 })
599 }
600
601 pub fn red(&self) -> i32 {
602 cpp!(unsafe [self as "const QColor*"] -> i32 as "int" {
603 return self->red();
604 })
605 }
606
607 pub fn red_f(&self) -> qreal {
608 cpp!(unsafe [self as "const QColor*"] -> qreal as "qreal" {
609 return self->redF();
610 })
611 }
612
613 pub fn rgb(&self) -> QRgb {
614 QRgb::from(cpp!(unsafe [self as "const QColor*"] -> u32 as "QRgb" {
615 return self->rgb();
616 }))
617 }
618
619 pub fn rgba64(&self) -> QRgba64 {
620 QRgba64::from(cpp!(unsafe [self as "const QColor*"] -> u64 as "QRgba64" {
621 return self->rgba64();
622 }))
623 }
624
625 pub fn rgba(&self) -> QRgb {
626 QRgb::from(cpp!(unsafe [self as "const QColor*"] -> u32 as "QRgb" {
627 return self->rgba();
628 }))
629 }
630
631 pub fn set_alpha(&mut self, alpha: i32) {
632 cpp!(unsafe [self as "QColor*", alpha as "int"] {
633 return self->setAlpha(alpha);
634 })
635 }
636
637 pub fn set_alpha_f(&mut self, alpha: qreal) {
638 cpp!(unsafe [self as "QColor*", alpha as "qreal"] {
639 return self->setAlphaF(alpha);
640 })
641 }
642
643 pub fn set_blue(&mut self, blue: i32) {
644 cpp!(unsafe [self as "QColor*", blue as "int"] {
645 return self->setBlue(blue);
646 })
647 }
648
649 pub fn set_blue_f(&mut self, blue: qreal) {
650 cpp!(unsafe [self as "QColor*", blue as "qreal"] {
651 return self->setBlueF(blue);
652 })
653 }
654
655 pub fn set_cmyk(&mut self, c: i32, m: i32, y: i32, k: i32, a: Option<i32>) {
656 let a = match a {
657 Some(a) => a,
658 None => 255,
659 };
660 cpp!(unsafe [self as "QColor*", c as "int", m as "int", y as "int", k as "int", a as "int"] {
661 return self->setCmyk(c, m, y, k, a);
662 })
663 }
664
665 pub fn set_cmyk_f(&mut self, c: qreal, m: qreal, y: qreal, k: qreal, a: Option<qreal>) {
666 let a = match a {
667 Some(a) => a,
668 None => 1.0,
669 };
670 cpp!(unsafe [self as "QColor*", c as "qreal", m as "qreal", y as "qreal", k as "qreal", a as "qreal"] {
671 return self->setCmykF(c, m, y, k, a);
672 })
673 }
674
675 pub fn set_green(&mut self, green: i32) {
676 cpp!(unsafe [self as "QColor*", green as "int"] {
677 return self->setGreen(green);
678 })
679 }
680
681 pub fn set_green_f(&mut self, green: qreal) {
682 cpp!(unsafe [self as "QColor*", green as "qreal"] {
683 return self->setGreenF(green);
684 })
685 }
686
687 pub fn set_hsl(&mut self, h: i32, s: i32, l: i32, a: Option<i32>) {
688 let a = match a {
689 Some(a) => a,
690 None => 255,
691 };
692 cpp!(unsafe [self as "QColor*", h as "int", s as "int", l as "int", a as "int"] {
693 return self->setHsl(h, s, l, a);
694 })
695 }
696
697 pub fn set_hsl_f(&mut self, h: qreal, s: qreal, l: qreal, a: Option<qreal>) {
698 let a = match a {
699 Some(a) => a,
700 None => 1.0,
701 };
702 cpp!(unsafe [self as "QColor*", h as "qreal", s as "qreal", l as "qreal", a as "qreal"] {
703 return self->setHslF(h, s, l, a);
704 })
705 }
706
707 pub fn set_hsv(&mut self, h: i32, s: i32, v: i32, a: Option<i32>) {
708 let a = match a {
709 Some(a) => a,
710 None => 255,
711 };
712 cpp!(unsafe [self as "QColor*", h as "int", s as "int", v as "int", a as "int"] {
713 return self->setHsv(h, s, v, a);
714 })
715 }
716
717 pub fn set_hsv_f(&mut self, h: qreal, s: qreal, v: qreal, a: Option<qreal>) {
718 let a = match a {
719 Some(a) => a,
720 None => 1.0,
721 };
722 cpp!(unsafe [self as "QColor*", h as "qreal", s as "qreal", v as "qreal", a as "qreal"] {
723 return self->setHsvF(h, s, v, a);
724 })
725 }
726
727 pub fn set_named_color(&mut self, name: &str) {
728 let len = name.len();
729 let ptr = name.as_ptr();
730 cpp!(unsafe [self as "QColor*", len as "size_t", ptr as "char*"] {
731 return self->setNamedColor(QLatin1String(ptr, len));
732 })
733 }
734
735 pub fn set_red(&mut self, red: i32) {
736 cpp!(unsafe [self as "QColor*", red as "int"] {
737 return self->setRed(red);
738 })
739 }
740
741 pub fn set_red_f(&mut self, red: qreal) {
742 cpp!(unsafe [self as "QColor*", red as "qreal"] {
743 return self->setRedF(red);
744 })
745 }
746
747 pub fn set_rgb(&mut self, r: i32, g: i32, b: i32, a: Option<i32>) {
748 let a = match a {
749 Some(a) => a,
750 None => 255,
751 };
752 cpp!(unsafe [self as "QColor*", r as "int", g as "int", b as "int", a as "int"] {
753 return self->setRgb(r, g, b, a);
754 })
755 }
756
757 pub fn set_qrgb(&mut self, rgb: QRgb) {
758 let rgb: u32 = rgb.0;
759 cpp!(unsafe [self as "QColor*", rgb as "QRgb"] {
760 return self->setRgb(rgb);
761 })
762 }
763
764 pub fn set_rgba_64(&mut self, rgba: QRgba64) {
765 cpp!(unsafe [self as "QColor*", rgba as "QRgba64"] {
766 return self->setRgba64(rgba);
767 })
768 }
769
770 pub fn set_rgb_f(&mut self, r: qreal, g: qreal, b: qreal, a: Option<qreal>) {
771 let a = match a {
772 Some(a) => a,
773 None => 1.0,
774 };
775 cpp!(unsafe [self as "QColor*", r as "qreal", g as "qreal", b as "qreal", a as "qreal"] {
776 return self->setRgbF(r, g, b, a);
777 })
778 }
779
780 pub fn set_rgba(&mut self, rgba: QRgb) {
781 let rgba: u32 = rgba.into();
782 cpp!(unsafe [self as "QColor*", rgba as "QRgb"] {
783 return self->setRgba(rgba);
784 })
785 }
786
787 pub fn spec(&self) -> QColorSpec {
788 cpp!(unsafe [self as "const QColor*"] -> QColorSpec as "QColor::Spec" { return self->spec(); })
789 }
790
791 pub fn to_cmyk(&self) -> QColor {
792 cpp!(unsafe [self as "const QColor*"] -> QColor as "QColor" {
793 return self->toCmyk();
794 })
795 }
796
797 // #[cfg(qt_5_14)]
798 // fn toExtendedRgb(&self) -> QColor {
799 // cpp!(unsafe [self as "const QColor*"] -> QColor as "QColor" {
800 // return self->toExtendedRgb();
801 // })
802 // }
803
804 pub fn to_hsl(&self) -> QColor {
805 cpp!(unsafe [self as "const QColor*"] -> QColor as "QColor" {
806 return self->toHsl();
807 })
808 }
809
810 pub fn to_hsv(&self) -> QColor {
811 cpp!(unsafe [self as "const QColor*"] -> QColor as "QColor" {
812 return self->toHsv();
813 })
814 }
815
816 pub fn to_rgb(&self) -> QColor {
817 cpp!(unsafe [self as "const QColor*"] -> QColor as "QColor" {
818 return self->toRgb();
819 })
820 }
821
822 pub fn value(&self) -> i32 {
823 cpp!(unsafe [self as "const QColor*"] -> i32 as "int" {
824 return self->value();
825 })
826 }
827
828 pub fn value_f(&self) -> qreal {
829 cpp!(unsafe [self as "const QColor*"] -> qreal as "qreal" {
830 return self->valueF();
831 })
832 }
833
834 pub fn yellow(&self) -> i32 {
835 cpp!(unsafe [self as "const QColor*"] -> i32 as "int" {
836 return self->yellow();
837 })
838 }
839
840 pub fn yellow_f(&self) -> qreal {
841 cpp!(unsafe [self as "const QColor*"] -> qreal as "qreal" {
842 return self->yellowF();
843 })
844 }
845}
846
847#[cfg(test)]
848mod tests {
849 use super::*;
850
851 #[test]
852 fn test_qcolor_from_name() {
853 let blue1 = QColor::from_name("blue");
854 let blue2 = QColor::from_rgb_f(0., 0., 1.);
855 assert_eq!(blue1.get_rgba_f().0, 0.);
856 assert_eq!(blue1.get_rgba_f().2, 1.);
857 assert!(blue1 == blue2);
858
859 let red1 = QColor::from_name("red");
860 let red2 = QColor::from_rgb_f(1., 0., 0.);
861 assert_eq!(red1.get_rgba_f().0, 1.);
862 assert_eq!(red1.get_rgba_f().2, 0.);
863 assert!(red1 == red2);
864 assert!(blue1 != red1);
865 }
866
867 #[test]
868 fn test_rgb() {
869 let color = QColor::from_rgb(255, 128, 0);
870 assert_eq!(255, color.red());
871 assert_eq!(128, color.green());
872 assert_eq!(0, color.blue());
873 assert_eq!(255, color.alpha());
874 assert_eq!((255, 128, 0, 255), color.get_rgba());
875 }
876
877 #[test]
878 fn test_cmyk() {
879 let mut color = QColor::from_cmyk(255, 200, 100, 0);
880 assert_eq!(255, color.cyan());
881 assert_eq!(200, color.magenta());
882 assert_eq!(100, color.yellow());
883 assert_eq!(0, color.black());
884 assert_eq!(255, color.alpha());
885 assert_eq!((255, 200, 100, 0, 255), color.get_cmyka());
886 }
887
888 #[test]
889 fn test_hsl() {
890 let color = QColor::from_hsla(255, 200, 100, 213);
891 assert_eq!(255, color.hsl_hue());
892 assert_eq!(200, color.hsl_saturation());
893 assert_eq!(100, color.lightness());
894 assert_eq!(213, color.alpha());
895 assert_eq!((255, 200, 100, 213), color.get_hsla());
896 }
897
898 #[test]
899 fn test_hsv() {
900 let color = QColor::from_hsva(255, 200, 100, 213);
901 assert_eq!(255, color.hsv_hue());
902 assert_eq!(200, color.hsv_saturation());
903 assert_eq!(100, color.value());
904 assert_eq!(213, color.alpha());
905 assert_eq!((255, 200, 100, 213), color.get_hsva());
906 }
907
908 #[test]
909 fn test_global_color() {
910 let red1 = QColor::from_global_color(QGlobalColor::Red);
911 let red2 = QColor::from_name("red");
912
913 assert_eq!(red1.get_rgba_f().0, 1.);
914 assert_eq!(red1.get_rgba_f().2, 0.);
915 assert!(red1 == red2);
916 }
917}
918