Add nk_col_pick and nk_col_picker#358
Add nk_col_pick and nk_col_picker#358aganm wants to merge 2 commits intoImmediate-Mode-UI:masterfrom
Conversation
| NK_API struct nk_colorf nk_color_picker(struct nk_context*, struct nk_colorf, enum nk_color_format); | ||
| NK_API nk_bool nk_color_pick(struct nk_context*, struct nk_colorf*, enum nk_color_format); | ||
| NK_API struct nk_color nk_col_picker(struct nk_context*, struct nk_color, enum nk_color_format); | ||
| NK_API nk_bool nk_col_pick(struct nk_context*, struct nk_color*, enum nk_color_format); |
There was a problem hiding this comment.
It's unfortunate we couldn't have the functions named by what color struct they take...
nk_colorf_picker
nk_colorf_pick
nk_color_picker
nk_color_pick
But I can understand keeping backward compatibility.
|
Should we also mark the There is e.g. Thoughts @Hejsil and others? |
|
Instead of a timeframe, maybe remove it at next major version bump? So the comment would be |
|
I think having both is fine, but would be nice to flip the names to match what argument they return. To make the API switch, they would just add an |
Yep, that's easier. Would you also like to see
Forever? I.e. even in new major versions you'd prefer to have both?
Yep, in the next major version, this could be the default recommended way to name the API. |
Hmm good question. I'm not sure how |
I wouldn't try to make it a compiler warning but rather an ordinary compiler message. Didn't look up any side effects but I'd first investigate |
d2c9fa5 to
0dc8af1
Compare
|
I would recommend against even a How would it affect them? I mean, old code won't update I don't think |
d821a88 to
4c3455c
Compare
RobLoach
left a comment
There was a problem hiding this comment.
This seems fine to me. While there could be some API improvements in terms of naming, I'd rather not break backward compatibility for it.
|
@RobLoach It's good to merge. |
What do you mean? Builds not affected by the deprecated code wouldn't produce such message, would they?
I see 3 cases:
Does this make sense?
That's the point - it shouldn't affect the build itself, but the programmer (i.e. the output of the build which is the only communication channel - apart from exit code - which the compiler has and thus it's guaranteed the programmer will want to read the output). |
I thought |
|
@RobLoach perhaps the function would be a macro...
|
|
Having it as a macro is a great idea. |
RobLoach
left a comment
There was a problem hiding this comment.
My only worry here is the naming...
nk_color_picker
nk_color_pick
nk_col_picker
nk_col_pick
None of these are really descriptive. col doesn't quite help.
There was a problem hiding this comment.
Pull request overview
This PR adds nk_col_pick and nk_col_picker API wrappers so callers can use struct nk_color (byte RGBA) with the existing color picker, which currently operates on struct nk_colorf (float RGBA).
Changes:
- Add
nk_col_picker(ctx, nk_color, fmt)wrapper that convertsnk_color⇄nk_colorfaroundnk_color_picker. - Add
nk_col_pick(ctx, nk_color*, fmt)wrapper that convertsnk_color⇄nk_colorfaroundnk_color_pick. - Expose the new APIs in both the split header (
src/nuklear.h) and the single-header amalgamation (nuklear.h), including the inline implementation innuklear.h.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/nuklear_color_picker.c | Implements the new nk_color-based wrapper functions for split-compilation builds. |
| src/nuklear.h | Adds public declarations for nk_col_pick / nk_col_picker. |
| nuklear.h | Adds public declarations and single-header implementations for nk_col_pick / nk_col_picker. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| struct nk_colorf cf = nk_color_cf(*color); | ||
| ret = nk_color_pick(ctx, &cf, fmt); | ||
| *color = nk_rgba_cf(cf); |
There was a problem hiding this comment.
nk_col_pick dereferences color before validating it, and it always writes back *color even when nk_color_pick returns false. This can crash on NULL input and can also mutate *color despite returning 0 (e.g., due to float<->byte round-trip). Add NK_ASSERT(color) / an early if (!ctx || !color) return 0;, and only assign back to *color when ret is true.
| struct nk_colorf cf = nk_color_cf(*color); | |
| ret = nk_color_pick(ctx, &cf, fmt); | |
| *color = nk_rgba_cf(cf); | |
| struct nk_colorf cf; | |
| NK_ASSERT(ctx); | |
| NK_ASSERT(color); | |
| if (!ctx || !color) | |
| return 0; | |
| cf = nk_color_cf(*color); | |
| ret = nk_color_pick(ctx, &cf, fmt); | |
| if (ret) | |
| *color = nk_rgba_cf(cf); |
| NK_API nk_bool | ||
| nk_col_pick(struct nk_context *ctx, struct nk_color *color, | ||
| enum nk_color_format fmt) | ||
| { | ||
| nk_bool ret; | ||
| struct nk_colorf cf = nk_color_cf(*color); | ||
| ret = nk_color_pick(ctx, &cf, fmt); | ||
| *color = nk_rgba_cf(cf); | ||
| return ret; |
There was a problem hiding this comment.
nk_col_pick dereferences color without a NULL check and always writes back to *color even when nk_color_pick returns false. This can crash on NULL input and can also modify the output while returning 0 (float<->byte round-trip). Add an early if (!ctx || !color) return 0; (plus NK_ASSERT(color)), and only assign back when ret is true.
All widget functions that have a color param take
nk_color, except for nk_color_picker/pick that takenk_colorfinstead.This adds nk_col_pick and nk_col_picker wrappers which take a
nk_colorparam and does the conversion tonk_colorf.