Globally or Locally change manim default font color for Tex and Text - manim

Manim Community v0.15.1
I am new to Manim.
I have been trying to change the font color of both Tex and Text objects without having to set the color for each Tex or Text object individually.
How do I change the font color for all Tex and Text objects within a scene or globally?
Here are some solutions that I believe I tried, but came up with nothing:
https://github.com/3b1b/manim/issues/1145
Change Text Colour Manim Community
I think this all has something to do with the following from Manim:
https://docs.manim.community/en/stable/installation/troubleshooting.html?highlight=config#config
Here is a code example I have:
from manim import *
class My_made_Up_Scene_Name(Scene):
def construct(self):
text_1 = Text("Text_1", font = "Arial", font_size = 50)
Where would I insert a scene-wide change of font color?

You can use the set_default method:
def construct(self):
Text.set_default(font="Arial", font_size=50)
text_1 = Text("Hello world!")

Like this:
class Example(Scene):
def construct(self):
Text.set_default(font = "Arial", font_size = 50)
t = Text("Text_1 Hello World")
t2 = Text("Foo, Bar").shift(DOWN)
self.add(t, t2)
https://docs.manim.community/en/v0.15.1/reference/manim.mobject.mobject.Mobject.html#manim.mobject.mobject.Mobject.set_default

Related

Text color is not set properly

Every time I want to set the text color, it doesn't work properly. For example, if I have a LazyColumn with some texts that have White color, some of them have white color, and some of them are set to Black.
Sometimes the text color is set properly but it changes by itself when I navigate through screens or when I swipe up or down.
code:
Text(
color = MaterialTheme.colors.textColor,
text = team.uppercase(Locale.getDefault()),
fontSize = 25.sp
)
val Colors.textColor
#Composable
get() = if(isSystemInDarkTheme()) Color.White else Color.DarkGray
Any help?

is there a way to change the background color of a specific word in outlined text field jetpack compose?

I am looking for a way to change the background color of certain words in outlined text field while typing, but I couldn't find a way to achieve that yet. Does anyone know a way to do this? (Preferably in compose but other solutions are also OK.)
You can use the VisualTransformation property in the TextField to change the text while typing. Use an AnnotatedString to apply different SpanStyle to your text.
Something like:
OutlinedTextField(
value = text,
onValueChange = { text = it },
visualTransformation = ColorsTransformation()
)
class ColorsTransformation() : VisualTransformation {
override fun filter(text: AnnotatedString): TransformedText {
return TransformedText(
buildAnnotatedStringWithColors(text.toString()),
OffsetMapping.Identity)
}
}
In the buildAnnotatedStringWithColors you can move your logic to change the background color of certain words. It is just an example to change the color for each word.
fun buildAnnotatedStringWithColors(text:String): AnnotatedString{
//it is just an example
val words: List<String> = text.split("\\s+".toRegex())// splits by whitespace
val builder = AnnotatedString.Builder()
val colors = listOf(Color.Red,Color.Black,Color.Yellow,Color.Blue)
var count = 0
for (word in words) {
//Use you favorite SpanStyle
builder.withStyle(style = SpanStyle(
background = colors[count % 4],
color = White
)) {
append("$word ")
}
count ++
}
return builder.toAnnotatedString()
}
The simplest solution is using annotated string:
Text(buildAnnotatedString {
append("Hello ")
withStyle(
style = SpanStyle(
fontWeight = FontWeight.Bold,
color = Color.White,
background = Color.Blue,
)
) {
append("World")
}
}, modifier = Modifier.padding(10.dp))
Result
Also take a look at this solution, which will allow you to use a more complex drawing like this:

Kivy: Change the background of ActionButton in ActionOverFlow

I want to change the background of the ActionButtons which are inside the ActionOverflow in an ActionBar, however using the usual approach for Buttons (background_down, background_normal) seems not to work.
[ActionButton with black background] (https://raw.githubusercontent.com/Thomas-Adams/kivy_mananger/master/color_mananger/kivy-actionbutton.png)
Strangely enough when i click on that specific ActionButton (for instance 'List') the background changes as desired. However the 'normal' not pressed state has still a black background. I also tried tampering with background-image property, but to no avail. What do I miss? Does anybody has a clue or hint about this?
#:import get_color_from_hex kivy.utils.get_color_from_hex
<MainView>:
...
ActionButton:
canvas.before:
Color:
rgba: get_color_from_hex('#d500f9')
Rectangle:
size: self.size
pos: self.pos
background_normal: 'electric-violet.png'
background_down: 'electric-violet-lighten.png'
background_disabled: ''
background_disabled_down: ''
background_color: get_color_from_hex('#d500f9')
text: 'Create'
Here's the link to the complete kv file
Another possibility is to create your own class based on ActionButton:
class MyActionButton(ActionButton):
real_background_normal = StringProperty('')
And modify its style in your kv file:
<MyActionButton>:
-background_normal: self.real_background_normal
Then elsewhere in your kv file, when you use MyActionButton, just use real_background_normal instead of background_normal:
MyActionButton:
real_background_normal: 'electric-violet.png'
I figured it out by myself, in case anybody else is stuck here, instead of using the ActionButton widget i wrote my own widget which is based on ActionItem and Button :
class ColorActionButton(ActionItem, Button, EventDispatcher):
color_name = StringProperty()
base_dir = IMAGE_BASE_DIR
b_normal = StringProperty()
b_down = StringProperty()
b_disabled_down = StringProperty()
b_disabled = StringProperty()
def __init__(self, **kwargs):
super().__init__(**kwargs)
def on_color_name(self, instance, value):
self.b_normal = self.base_dir + value + SUFFIX
self.b_down = self.base_dir + value + LIGHTEN_SUFFIX
self.b_disabled_down = self.base_dir + value + DARKEN_SUFFIX
Using this now works like a charm.

Kivy window set_title changes back to main file name

When I set the window title, the title is set back to it's corresponding file name.
class myniceApp(App):
global Window
def build(self):
Window.clearcolor = (.95,.95,.95,1)
Window.size = (1024, 768)
Window.set_title('mykivyapp')
Builder.load_string(style)
homewin = MyniceappHome()
homewin.initapp()
return homewin
myniceApp().run()
In the above example, the title 'mykivyapp' is shown initially but set back to the filename after homewin.initapp()
How should set_title() be used?
Window title is set with App.title, not with Window directly:
class MyApp(App):
def build(self):
self.title = 'Hello world'
you can use it like below.. this one worked for me.. i tried the first solution explained above, but it did not work.. so i change a bit and put the title outside the build function, its working successfully..
class controllerApp(App):
title = 'Vehicle Detection System'
def build(self, video_source=VIDEO_SOURCE):
...
...

on_touch_down problems with dynamic created widgets in kivy

I'm new to kivy and need some assistance with the following problem. I adding numbers/operators as widgets (labels) dynamically and at a random position to a layout(FloatLayout). The idea is that when I clicked on a number/operator it will draw a circle around the number/operator. I get some very strange behaviour. Does not matter what number/operator I click the selection circle is only drawn around the last added label. Then to confuse me even more the other number/operator is circled if I press on some random point on the screen
following is the core of my code:
class SelectedObject(Label):
selected = BooleanProperty()
refresh = BooleanProperty()
def __init__(self, **kwargs):
super(SelectedObject, self).__init__(**kwargs)
self.center_x = randint(0, Window.width/2)
self.center_y = randint(0, Window.height/2)
self.bind( refresh = self.redraw )
def redraw(self, *args):
#print('Redraw for: ' + self)
self.canvas.after.clear()
if self.selected:
with self.canvas.after:
Line(circle=(self.center_x, self.center_y, 20))
self.canvas.ask_update()
def on_touch_down(self, touch):
print("touch#: " + str(touch))
if not self.collide_point(touch.x, touch.y):
return False
print("yip: " + self)
self.selected = not self.selected
self.refresh = not self.refresh # force a redraw
return True
class GameNumber(SelectedObject):
pass
class Operator(SelectedObject):
pass
class GameApp(App):
numberArr = ListProperty([])
operatorArr = ListProperty([])
def build(self):
f = FloatLayout()
#populate numberArr and operatorArr
self.buildLevel()
for number in self.numberArr:
numberItem = GameNumber(text = str(number))
f.add_widget(numberItem)
for operator in self.operatorArr:
f.add_widget(Operator(text = operator))
return f
The problem here is that you have not set sizes to the labels. So each label takes up as much space as it can and the last label being on top, it gets the circle.
You need to pass each GameNumber and Operator some sort of size_hint and/or size. For example, if you want each label to be 10 by 10, you can do something like this: numberItem = GameNumber(text=str(number), size_hint=(None, None), size=(10, 10)). You can set their size relative to the window size by for example setting size_hint=(0.1, 0.1).
Remember that size_hint is always (1, 1) by default, so you need to change it something else if you want your widgets to be smaller than the space of the container and set size_hint=(None, None) if you want to set a fixed size yourself.
If you want to position the labels randomly around the screen, take a look at pos_hint, which is more convenient than playing with the size of the window directly. Also, remember that there's a chance that multiple labels might get on top of each other or at the very borders of the window or even outside if you are not careful.
EDIT: To help you find the source of these kind of problems (that usually relate to layout issues), take a look at Kivy Widget Area Display

Resources