How to convert from GDKColor to GDKRGBA struct? - gtk3

I'm converting some code from GdkColor to GdkRGBA. This the the GTK+2 code:
f_colors = img->current_slide->font_brdr_color;
color.red = (gint)( f_colors[0] * 0xffff );
color.green = (gint)( f_colors[1] * 0xffff );
color.blue = (gint)( f_colors[2] * 0xffff );
gtk_color_button_set_color( GTK_COLOR_BUTTON( img->sub_brdr_color ), &color );
gtk_color_button_set_alpha( GTK_COLOR_BUTTON( img->sub_brdr_color ),
(gint)(f_colors[3] * 0xffff ) );
color is a GdkRGBA struct while f_colors is an array of double. The code perfectly work as the color button is set to the color I use but if I turn the code into GTK+3 the color of gtk_color_chooser doesn't reflect the color I use.
How can I convert the array of doubles into a GdkRGBA struct?
Many thanks

Ok I found it!
f_colors = img->current_slide->font_brdr_color;
color.red = f_colors[0];
color.green = f_colors[1];
color.blue = f_colors[2];

Related

How to convert string to color in Flutter

I want to convert the string to color but I can't,
My Code
String color = "0xffff5252";
Color result= Color(int.parse(color, radix: 16));
print("$result");
** I get the following problem**
Invalid radix-16 number (at character 1) 0xffff5252
You can try something like below
String color = "0xffff5252";
Color result= Color(int.parse(color));
print(result);
if youe motive is to define a color by first defining into string and then by parsing it, then there's a much simpler way to define color,
const Color primary = Color(0xFF92A3FD);
const Color secondary = Color(0xFF9DCEFF);
const Color thirdColor = Color(0xFFC58BF2);
const Color fourthColor = Color(0xFFEEA4CE);
const Color bgTextField = Color(0xFFF7F8F8);
const Color facebookColor = Color(0xFF3b5998);
const Color googleColor = Color(0xFFDB4437);
Try this hexToColor function:
// Construct a color from a hex code string, of the format RRGGBB.
Color hexToColor(String code) {
return Color(int.parse(code.substring(1, 6), radix: 16) + 0xFF000000);
}
This worked for me to store a color & use it as a profile avatar color.

How do I access a custom color that is part of a struct?

I'm trying to assign a color to the following button that I gave to a member of a struct previously, yet I keep getting compiler errors. Here's what my code is:
var sampleGameView: gameViewState = gameViewState(
Outs: 2,
Balls: 1,
Strikes: 1,
Runner1: true,
Runner2: true,
Runner3: false,
HomeTeam: "MIA",
AwayTeam: "ATL",
HomeTeamColor: Color("SampleGameViewATL"),
AwayTeamColor: Color("SampleGameViewMIA"),
InningTop: false,
InningNumber: 4,
HomeTeamScore: 0,
AwayTeamScore: 1,
CurrentBatterFirst: "Starling",
CurrentBatterLast: "Marte",
CurrentPitcherFirst: "Max",
CurrentPitcherLast: "Fried"
)
As you can see, sampleGameView has two Color properties. However, when I try to assign them to a Button like so, I get errors:
Button(action: {}) {
VStack {
Text(sampleGameView.CurrentPitcherFirst)
Text(sampleGameView.CurrentPitcherLast)
.fontWeight(.bold)
.textCase(.uppercase)
}
.padding()
}
.foregroundColor(.white)
.frame(width: 170, height: 75)
.background(Color("sampleGameView.HomeTeamColor")) // error is on this line
.cornerRadius(10)
I've tried accessing the color without quotes at all, and also with string interpolation, neither of which works :/
Can someone tell me what the correct syntax is for a situation like this? Thank you!
Remove double quotes and Color object constructor.
.background(sampleGameView.HomeTeamColor)
Note: Struct and Class name start with capital character and properties start with small character.

Randomize swatch for a color passed to a function in Flutter

I have a function that gets Color as an argument. I want to generate a list of random swatches for that color within that function. For example, if the color passed is Colors.amber I want a list like:
[ Colors.amber[100], Colors.amber[800], Colors.amber[500], Colors.amber[900], Colors.amber[300]... ]
Is it possible? Any advice or help would be appreciated. Thank you in advance.
You can randomize only the list of "plus colors":
Flutter Colors
List<int> types = [50, 100, 200, 300, 400, 600, 700, 800, 900]
And randomize a number between 0..8
int get getRandomNumber => 0 + Random().nextInt(8 - 0);
Use to get the random color
Color? selectedColor = Colors.amber[types[getRandomNumber]];
Update:
Using this method extension, we can create a "workaround"
extension HexColor on Color {
/// String is in the format "aabbcc" or "ffaabbcc" with an optional leading "#".
Color fromHex(String hexString) {
final buffer = StringBuffer();
if (hexString.length == 6 || hexString.length == 7) buffer.write('ff');
buffer.write(hexString.replaceFirst('#', ''));
return Color(int.parse(buffer.toString(), radix: 16));
}
/// Prefixes a hash sign if [leadingHashSign] is set to `true` (default is `true`).
String toHex({bool leadingHashSign = true}) => '${leadingHashSign ? '#' : ''}'
'${alpha.toRadixString(16).padLeft(2, '0')}'
'${red.toRadixString(16).padLeft(2, '0')}'
'${green.toRadixString(16).padLeft(2, '0')}'
'${blue.toRadixString(16).padLeft(2, '0')}';
}
And using this function
Color getRandomColor(String selectedColor) {
final List<int> types = [50, 100, 200, 300, 400, 600, 700, 800, 900];
int getRandomNumber = 0 + Random().nextInt(8 - 0);
return Color.fromHex(selectedColor)[types[getRandomNumber]];
}
//true will return "#ffffff", false will return "ffffff"
Color _color = getRandomColor(Color.red.toHex(true));
Found a simple solution
Let color be the passed parameter. Now find the index of that color in the Colors.primaries list.
for(int i=0;i<Colors.primaries.length;i++){
if(color==Colors.primaries[i]){
colorIndex=i;
}
}
Use this index to generate the random swatch of the preferred color.
Colors.primaries[colorIndex][Random.nextInt(9) * 100]

Unable to pass parameter from command to converter

I'm trying to edit the actual existing plugin of highlight to be able to add text color customizable from command argument, which means that if you put RGB or hex color it will take it on the selection create a view attribute style = 'color: PARAMETER'
What I got so far just surround the text with font tag and the style attribute with color but with a color value incorrect.
Which mean the value on the command is not being passed to the converter.
I'm being unable to find a way to transfer input information from a command to use directly in the converter to make this feature works.
The normal behavior in this plugin make it impossible to use the 16 million colors from RGB, and it requires an obsolete model of defined colors.
You can see/edit/use what i've done so far in this repo
https://github.com/klys/ckeditor5-highlight-inline
In this part the converter create the font element with the style color but with a incorrect value for color value.
editor.conversion.attributeToElement( {
model: HIGHLIGHT,
view: {
name: 'font',
styles: {
'color': true
},
priority: 5,
model: {
key: 'highlight',
value: viewElement => viewElement.getAttribute( 'color' )
},
type:'pen'
}
} );
And this the edited command executor from the original plugin, which i edit to meet the needs of this feature.
execute( color ) {
const model = this.editor.model;
const document = model.document;
const selection = document.selection;
//const highlighter = options.value;
/*var __model;
for ( const option of options ) {
__model = new Model( {
model: 'color model',
class: 'color-class',
title: 'color pen',
color: option.value,
type: 'pen'
} );
}
const highlighter = __model;*/
model.change( writer => {
const ranges = model.schema.getValidRanges( selection.getRanges(), 'highlight' );
if ( selection.isCollapsed ) {
const position = selection.getFirstPosition();
// When selection is inside text with `highlight` attribute.
if ( selection.hasAttribute( 'highlight' ) ) {
// Find the full highlighted range.
const isSameHighlight = value => {
return value.item.hasAttribute( 'highlight' ) && value.item.getAttribute( 'highlight' ) === this.value;
};
const highlightStart = position.getLastMatchingPosition( isSameHighlight, { direction: 'backward' } );
const highlightEnd = position.getLastMatchingPosition( isSameHighlight );
const highlightRange = writer.createRange( highlightStart, highlightEnd );
// Then depending on current value...
if ( !color || this.value === color ) {
// ...remove attribute when passing highlighter different then current or executing "eraser".
writer.removeAttribute( 'highlight', highlightRange );
writer.removeSelectionAttribute( 'highlight' );
} else {
// ...update `highlight` value.
writer.setAttribute( 'highlight', color, highlightRange );
writer.setSelectionAttribute( 'highlight', color );
}
} else if ( color ) {
writer.setSelectionAttribute( 'highlight', color );
}
} else {
for ( const range of ranges ) {
if ( color ) {
writer.setAttribute( 'highlight', color, range );
} else {
writer.removeAttribute( 'highlight', range );
}
}
}
} );
}
I have been almost a week in this problem.
If you can a least point me out in the right direction, you won't imagine how much you will help me.

Postprossing error - copyTexImage2D: framebuffer is incompatible format

Postprossing error - copyTexImage2D: framebuffer is incompatible format
I'm trying to use the post effects as presented in the samples, but I keep getting the following error in my console and nothing is rendering. Any ideas on what might be causing this? I don't think anything that matters is different than in the postprocessing example.
WebGL: INVALID_OPERATION: copyTexImage2D: framebuffer is incompatible format
renderer = new THREE.WebGLRenderer()
renderer.setSize(window.innerWidth, window.innerHeight)
renderer.setClearColorHex(0x000000, 1)
renderer.sortObjects = false;
...
composer = new THREE.EffectComposer( renderer )
composer.addPass( new THREE.RenderPass( scene, camera ) )
effect = new THREE.ShaderPass( THREE.DotScreenShader )
effect.uniforms[ 'scale' ].value = 4
composer.addPass( effect )
effect = new THREE.ShaderPass( THREE.FXAAShader )
composer.addPass( effect )
render = ->
requestAnimationFrame(render)
composer.render()
render()
You could try to add alpha to the WebGLRenderer default framebuffer using:
renderer = new THREE.WebGLRenderer( { antialias: true, alpha: true } );
I found out the error, there's a bug where EffectComposer is incompatable with LensFlare, submitting a change.
Basing on #Axiverse suggest, if your problem come with usage of lens flare (or maybe anything else...), simply add your own render target.
//...
var pixelRatio = renderer.getPixelRatio();
var width = Math.floor( renderer.context.canvas.width / pixelRatio ) || 1;
var height = Math.floor( renderer.context.canvas.height / pixelRatio ) || 1;
var renderTarget = new THREE.WebGLRenderTarget(width, height, {
minFilter: THREE.LinearFilter,
magFilter: THREE.LinearFilter,
format: THREE.RGBAFormat, // <-- The line that fix all for me
stencilBuffer: false
});
var effectComposer = new THREE.EffectComposer(webGLRenderer, renderTarget);