Skip to content

Commit a32bddd

Browse files
committed
Bind r8 g8 b8 a8 and h s v properties of Color
1 parent e793090 commit a32bddd

File tree

2 files changed

+71
-7
lines changed

2 files changed

+71
-7
lines changed

misc/godot.d.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -900,25 +900,25 @@ declare module godot {
900900
a: number;
901901

902902
/** HSV hue value (range 0 to 1). */
903-
// h: number;
903+
h: number;
904904

905905
/** HSV saturation value (range 0 to 1). */
906-
// s: number;
906+
s: number;
907907

908908
/** HSV value (range 0 to 1). */
909-
// v: number;
909+
v: number;
910910

911911
/** Red value (range 0 to 255). */
912-
// r8: number;
912+
r8: number;
913913

914914
/** Green value (range 0 to 255). */
915-
// g8: number;
915+
g8: number;
916916

917917
/** Blue value (range 0 to 255). */
918-
// b8: number;
918+
b8: number;
919919

920920
/** Alpha value (range 0 to 255). */
921-
// a8: number;
921+
a8: number;
922922

923923

924924
/** Returns a new color resulting from blending this color over another. If the color is opaque, the result is also opaque. The second color may have a range of alpha values.

quickjs/quickjs_builtin_binder.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,70 @@ JSValue QuickJSBuiltinBinder::new_object_from(JSContext *ctx, const PoolVector3A
630630

631631
void QuickJSBuiltinBinder::bind_builtin_propties_manually() {
632632

633+
{ // Color
634+
JSCFunctionMagic *getter = [](JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv, int magic) -> JSValue {
635+
ECMAScriptGCHandler *bind = BINDING_DATA_FROM_JS(ctx, this_val);
636+
const Color *ptr = bind->getColor();
637+
switch (magic) {
638+
case 0:
639+
return QuickJSBinder::to_js_number(ctx, Math::round(ptr->r * 255));
640+
case 1:
641+
return QuickJSBinder::to_js_number(ctx, Math::round(ptr->g * 255));
642+
case 2:
643+
return QuickJSBinder::to_js_number(ctx, Math::round(ptr->b * 255));
644+
case 3:
645+
return QuickJSBinder::to_js_number(ctx, Math::round(ptr->a * 255));
646+
case 4:
647+
return QuickJSBinder::to_js_number(ctx, ptr->get_h());
648+
case 5:
649+
return QuickJSBinder::to_js_number(ctx, ptr->get_s());
650+
case 6:
651+
return QuickJSBinder::to_js_number(ctx, ptr->get_v());
652+
}
653+
return JS_UNDEFINED;
654+
};
655+
656+
JSCFunctionMagic *setter = [](JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv, int magic) -> JSValue {
657+
ECMAScriptGCHandler *bind = BINDING_DATA_FROM_JS(ctx, this_val);
658+
Color *ptr = bind->getColor();
659+
#ifdef DEBUG_METHODS_ENABLED
660+
ERR_FAIL_COND_V(!QuickJSBinder::validate_type(ctx, Variant::REAL, argv[0]), (JS_ThrowTypeError(ctx, "number value expected")));
661+
#endif
662+
real_t value = QuickJSBinder::js_to_number(ctx, argv[0]);
663+
switch (magic) {
664+
case 0:
665+
ptr->r = value / 255.0;
666+
break;
667+
case 1:
668+
ptr->g = value / 255.0;
669+
break;
670+
case 2:
671+
ptr->b = value / 255.0;
672+
break;
673+
case 3:
674+
ptr->a = value / 255.0;
675+
break;
676+
case 4:
677+
ptr->set_hsv(value, ptr->get_s(), ptr->get_v(), ptr->a);
678+
break;
679+
case 5:
680+
ptr->set_hsv(ptr->get_h(), value, ptr->get_v(), ptr->a);
681+
break;
682+
case 6:
683+
ptr->set_hsv(ptr->get_h(), ptr->get_s(), value, ptr->a);
684+
break;
685+
}
686+
return JS_DupValue(ctx, argv[0]);
687+
};
688+
binder->get_builtin_binder().register_property(Variant::COLOR, "r8", getter, setter, 0);
689+
binder->get_builtin_binder().register_property(Variant::COLOR, "g8", getter, setter, 1);
690+
binder->get_builtin_binder().register_property(Variant::COLOR, "b8", getter, setter, 2);
691+
binder->get_builtin_binder().register_property(Variant::COLOR, "a8", getter, setter, 3);
692+
binder->get_builtin_binder().register_property(Variant::COLOR, "h", getter, setter, 4);
693+
binder->get_builtin_binder().register_property(Variant::COLOR, "s", getter, setter, 5);
694+
binder->get_builtin_binder().register_property(Variant::COLOR, "v", getter, setter, 6);
695+
}
696+
633697
{ // PoolByteArray
634698
// PoolByteArray.prototype.compress
635699
binder->get_builtin_binder().register_method(

0 commit comments

Comments
 (0)